Time for another casual little challenge.
For this one there are only 2 rules:
- you must add numbers
a
andb
together - you must NOT use the
+-*/
operators
Apart from that there are no more rules, and can be done with any language you want!
Pseudocode:
a = 2
b = 32
add(a, b) => 34
Test:
add(a, b) == a + b
Have fun! Let's see what you come up with!
Top comments (32)
Compile time execution goes brrrrrr 😂
Here is the full code with other solutions: https://play.rust-lang.org/?version=stable&mode=release&edition=2021&gist=3fd17ceb138f1e2c230987bd93d93f25
Can you explain the trick with size?
The memory size of the struct
_Add
here would be the size of_a
and_b
combined, in other words for any typeT
and length ofn
,[T; n]
has the size ofn * size_of::<T>()
, and since hereT
isu8
and its size is just1
byte; hence[u8; n]
is justn
bytes. Using this; the size of_Add
would be the size of[u8; A]
(A bytes) and the size of[u8; B]
(B bytes); will result in total isA+B
bytes.Yes but this is where I'm lost. Wouldn't be the returned value 2 in every case?
No, in the test case,
A = 40
means[0u8; 40]
that also means40 bytes
, andB = 2
which is[0u8; 2]
that's 2 bytes, which is total of42
bytes.Ah indeed, thanks! I misunderstood the syntax.
edit : that's a really smart solution btw, thanks for sharing
Rust gang lesssgooo
Nice, that's really interesting! I like how the compiler replaces the Iterator example with
a + b
lol 🤣Crazy!
(for anyone wonder what iterators, check out the rust playground link I've posted, there is other solution that uses iterators instead of const generics)
Thats a nice logical solution
I mean, it looks cuter without
if
orreturn
:Wise solution 😜
Crystal or Ruby
Here's a really nasty one:
Cheers!
PS: Never do this. Is disgusting and it doesn't even add float numbers
It kinda is nasty 🤣 I like it though
I spent an unfortunate amount of time on this...
Output:
This currently only works on unsigned integers. I don't feel like spending the time now to remember how two's complement works.
Though I guess I'll be a bit more pedantic with this one:
for i in 0..32
smuggles in some integer addition. So here's the set theoretic implementation, which has—less reliance on plus signs:Output:
Z means zero, and S means increment. So this is zero incremented eight times.
Thank you for the challenge, I finally got into WebAssembly 🎉
Here's another, more serious, attempt (still in Ruby) :
Some explanations : in Ruby
0..10
is called a range. It is really useful to make arbitrary loops or splice an array, for example. Simply, a range is a suite of values. The syntax I used here is0...10
(note the three dots) which is an exclusive range : the suite of values goes from0
to9
. So the trick is to have a range going from-a
tob
and excludingb
because0
is included in the suite.In python
Nice cheat :p
This is clearly cheating, but here we go with a Ruby solution :D
Hahahah true xD if we stick to the rules yup... but I think I'll pass this one. I guessed there would be some languages that had built-in methods like this! What's the language BTW?
Ruby and, by extension, Crystal.
Nice one!! I also thought of this solution!!! Not very scalable but cool nonetheless
js
Dangerous one:
A bit shorter (if you don't mind polluting namespace):