Time for another casual little challenge.
For this one there are only 2 rules:
- you must add numbers
aandbtogether - 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 (30)
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
_Addhere would be the size of_aand_bcombined, in other words for any typeTand length ofn,[T; n]has the size ofn * size_of::<T>(), and since hereTisu8and its size is just1byte; hence[u8; n]is justnbytes. Using this; the size of_Addwould be the size of[u8; A](A bytes) and the size of[u8; B](B bytes); will result in total isA+Bbytes.Yes but this is where I'm lost. Wouldn't be the returned value 2 in every case?
No, in the test case,
A = 40means[0u8; 40]that also means40 bytes, andB = 2which is[0u8; 2]that's 2 bytes, which is total of42bytes.Ah indeed, thanks! I misunderstood the syntax.
edit : that's a really smart solution btw, thanks for sharing
Nice, that's really interesting! I like how the compiler replaces the Iterator example with
a + blol š¤£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)
Rust gang lesssgooo
Thats a nice logical solution
Wise solution š
Crystal or Ruby
Thank you for the challenge, I finally got into WebAssembly š
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.
In python
Nice cheat :p
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..32smuggles 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.
Here's another, more serious, attempt (still in Ruby) :
Some explanations : in Ruby
0..10is 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 from0to9. So the trick is to have a range going from-atoband excludingbbecause0is included in the suite.Nice one!! I also thought of this solution!!! Not very scalable but cool nonetheless