Live data from Hacker News

Write a program that makes 2 + 2 = 5

codegolf.stackexchange.com

31–40 of 76 posts

Re: Write a program that makes 2 + 2 = 5

#33
post #18

C: #include #include #include int nums[5] = {1, 0, 1, 0, 1}; static int cur; int main(int argc, char **argv) { for(int i = 1; nums[i] != 5 && i No strange array writes! No indirect writes at all! It's finicky and I believe depends on register allocation, but when I compile it using whatever version of gcc-mp-4.9 I have installed from MacPorts at -O3, it outputs, among other things: 2 + 2 == 5 = true (For all they say…

Can anyone explain? It didn't work for me but still gives odd results depending on optimization level that I don't understand.

Re: Write a program that makes 2 + 2 = 5

#34
post #17

Earlier quoted context omitted.

I think the line above the printf call should be arr[2] = 3; I ran your program and I got 4. Then I changed arr[1] to arr[2] and got 5, as I expected.

It's only likely, but not certain that a or b get overwritten. How your stack is laid out is entirely up to the compiler. arr[1] is already out of bounds, however, we don't know for certain what's immediately above the array.

With compiler optimizations turned on, it's almost guaranteed not to happen, because a and b are very likely to be stored in CPU registers.

Re: Write a program that makes 2 + 2 = 5

#35
post #28

Asking: Isn't the only true solution the one using Decibels for Addition? Because everyone else either just redefines or overwrites a value. Also addition of decibels cannot be disproofed as fake.

After thinking for a while, I found that there is no true answer. If we are not dealing with normal calculating, every answer should redefine the calculating system or value in a tricky way. The simplest answer I can tell related to the addition of decibels is like this:

    function add(a, b): return 5
But it is just not so fun.

Re: Write a program that makes 2 + 2 = 5

#36
post #23

Here's mine : $ cat test.c #include int main() { int a = 3; int b = 3; // aren't we supposed to add 2 and 2 ??/ a = 2; b = 2; printf("%d\n", a + b); return 0; } $ gcc -W -Wall -trigraphs test2.c 2>/dev/null $ ./a.out 5 $

Very sneaky. For anyone else wondering why this works, look up trigraphs.

Re: Write a program that makes 2 + 2 = 5

#37
post #33
post #18

C: #include #include #include int nums[5] = {1, 0, 1, 0, 1}; static int cur; int main(int argc, char **argv) { for(int i = 1; nums[i] != 5 && i No strange array writes! No indirect writes at all! It's finicky and I believe depends on register allocation, but when I compile it using whatever version of gcc-mp-4.9 I have installed from MacPorts at -O3, it outputs, among other things: 2 + 2 == 5 = true (For all they say…

Can anyone explain? It didn't work for me but still gives odd results depending on optimization level that I don't understand.

Because nums is a length 5 array, and the loop condition checks nums[i], GCC assumes going into the loop that i is a valid index, i.e. i It's based on an old Linux bug... I should learn more about GCC internals in order to tell tell why I couldn't get it to work without a loop. In general, any sequence where some property not holding would cause undefined behavior to occur allows a conforming compiler to assume it does hold, but for most basic examples neither GCC nor Clang does anything special.

Re: Write a program that makes 2 + 2 = 5

#38
I'm surprised no one has suggested this yet. In python

>Write a program that seemingly adds the numbers 2 and 2

a=2+2

>and outputs 5.

print(5)

Yeah, I know that isn't the intent here, but, following the wording of the contest rules exactly...

Re: Write a program that makes 2 + 2 = 5

#39
post #23

Here's mine : $ cat test.c #include int main() { int a = 3; int b = 3; // aren't we supposed to add 2 and 2 ??/ a = 2; b = 2; printf("%d\n", a + b); return 0; } $ gcc -W -Wall -trigraphs test2.c 2>/dev/null $ ./a.out 5 $

Is the trick here the "-trigraphs" flag, or the fact that you are showing us test.c but compiling test2.c? Both approaches seem like they should work.
Post reply on HN