I would like to post on there but it's protected from people with low reputation... Maybe someone here appreciates my solution in C:<p><pre><code> #include <stdio.h>
int main(int argc, char* argv[]){
int arr[1];
int a = 2;
int b = 2;
arr[1] = 3;
printf("%d", a+b);
return 0;
}
</code></pre>
Explanation: I go out of bounds of the array arr, it only has one value but I access the second value. That's why b is likely to get overwritten with 3 and hence a+b=5
Here's mine :<p><pre><code> $ cat test.c
#include <stdio.h>
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
$</code></pre>
That Java one is particularly amusing, as most programmers otherwise familiar with the language would never know about the <i>integer cache</i> (and their reaction upon discovering that there is one would probably be the same as mine - <i>WTF?</i>)<p>Edit: I had a feeling I'd heard of an "integer cache" somewhere else" in a WTF-eliciting context... <a href="http://thedailywtf.com/Articles/The-Integer-Cache.aspx" rel="nofollow">http://thedailywtf.com/Articles/The-Integer-Cache.aspx</a> !
UB abuse in C/C++ Linux x86-64 (works with gcc/clang/icc with any optimization level):<p><pre><code> #include <stdio.h>
int main() {
double x;
printf("Input any number:\n> ");
if (scanf("%lf", &x) == 1) {
printf("Is 2 + 2 really equals %g? Let's try!\n", x);
printf("2 + 2 = %g\n", 2 + 2);
} else {
printf("Invalid input!\n");
}
}
</code></pre>
Output:<p><pre><code> Input any number:
> 5
Is 2 + 2 really equals 5? Let's try!
2 + 2 = 5
</code></pre>
Explanation: linux x86-64 calling convention uses xmm registers to pass fp values. In the first printf we initialize %xmm0 with some value. In the second printf we put integer 4 in %esi, however printf reads value again from %xmm0. Here is an assembly in GCC explorer (sorry for shortened link, that's how GCC explorer works): <a href="http://goo.gl/mY9phE" rel="nofollow">http://goo.gl/mY9phE</a>
Here's a Ruby solution that really does modify the + operation:<p><pre><code> class Fixnum
alias_method :old_plus, :+
def +(*args)
old_plus(*args).old_plus(1)
end
end
2 + 2 #=> 5</code></pre>
<p><pre><code> public class Main {
public static void main (String [] args) {
System.out.println("5=2+2"); // prints 4=2+2
}
}</code></pre>
C:<p><pre><code> #include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
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 < 10; i++) {
cur = i;
if(i/4 == 1) {
printf("2 + 2 <= %d = %s\n", cur, 2 + 2 <= i ? "true" : "false");
printf("2 + 2 == %d = %s\n", cur, 2 + 2 == i ? "true" : "false");
printf("\n");
}
}
}
</code></pre>
No strange array writes! No indirect writes at all!<p>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:<p><pre><code> 2 + 2 == 5 = true
</code></pre>
(For all they say about evil optimizing compilers, it was really hard to make this work.)
Ruby solution (don't have enough rep to post to codegolf):<p><pre><code> def mad_addr(a,b)
a == b && a == 2 ? (a.object_id + b.object_id)/a : a + b
end
</code></pre>
Explanation: If a and b are both equal to 2 we can sum the values of the object_id's (the object_id of 2 is 5), and then divide the sum by 2. [Edit to add] For all other numbers this should behave as expected.<p>I just learned the object_id of 2 is 5 while doing this exercise, and I would like to continue learning more about how Ruby works. So if you have feedback, criticisms, or other ideas regarding this solution please feel free to share :)<p>[edit: added missing `=`]
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.
You know, this would be possible in C and C++ if you add some code at the start of main() that unprotects the executable section, goes through it and modifies each "load 4 in register X" instruction to "load 5", then forks a process that attaches itself as a debugger to the executable to monitor every add and change the result to 5 if it's 4.
Lua:<p><pre><code> local debug = require "debug" -- in case 5.2
debug.setmetatable(1, {__tostring=function(n)
return ("%g"):format(n+1)
end})
print(2+2)
</code></pre>
Trying to make 2+2==5 evaluate as true is a lot harder. Although Lua lets you define a custom + operator, it's a fallback not an override.
How about the opposite? Make 2+3=4, here in C.<p><pre><code> add2And3(int *a, int *b)
{
*a = 3;
*b = 2;
printf("%d", *a + *b); //4
}
void main()
{
int storage;
add2And3(&storage, &storage);
}</code></pre>
<p><pre><code> #!/bin/bash
tty
if [[ $? -ne 0 ]] ; then
# command returned nonzero
echo "not a tty"
else
# command returned zero
n=$?
((n=$n+2))
((n=$n+2))
echo "0+2+2=$n";
fi</code></pre>
Combining a couple ideas, here's a Go solution<p><a href="http://play.golang.org/p/XnPglbkLW4" rel="nofollow">http://play.golang.org/p/XnPglbkLW4</a>
I'm surprised no one has suggested this yet. In python<p>>Write a program that seemingly adds the numbers 2 and 2<p>a=2+2<p>>and outputs 5.<p>print(5)<p>Yeah, I know that isn't the intent here, but, following the wording of the contest rules exactly...