Live data from Hacker News

Write a program that makes 2 + 2 = 5

codegolf.stackexchange.com

11–20 of 76 posts

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

#12

That Java one is particularly amusing, as most programmers otherwise familiar with the language would never know about the integer cache (and their reaction upon discovering that there is one would probably be the same as mine - WTF? ) Edit: I had a feeling I'd heard of an "integer cache" somewhere else" in a WTF-eliciting context... http://thedailywtf.com/Articles/The-Integer-Cache.aspx !

It reminds me of a prank a relative of mine used to play on other people using punch-card computers. The computer had no math unit (!) so addition tables and multiplication tables were fed in on punch cards. Easy enough to change one of those and make perfect code give very odd answers.

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

#13

Here's a Ruby solution that really does modify the + operation: class Fixnum alias_method :old_plus, :+ def +(*args) old_plus(*args).old_plus(1) end end 2 + 2 #=> 5

Here's one in Ocaml that modifies the + operator

let (+) x y = match (x,y) with (2,2) -> 5 | (x,y) -> x+y;;

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

#14
post #12

That Java one is particularly amusing, as most programmers otherwise familiar with the language would never know about the integer cache (and their reaction upon discovering that there is one would probably be the same as mine - WTF? ) Edit: I had a feeling I'd heard of an "integer cache" somewhere else" in a WTF-eliciting context... http://thedailywtf.com/Articles/The-Integer-Cache.aspx !

It reminds me of a prank a relative of mine used to play on other people using punch-card computers. The computer had no math unit (!) so addition tables and multiplication tables were fed in on punch cards. Easy enough to change one of those and make perfect code give very odd answers.

Boy, he must've been hated, I personally am too young to remember the punch-card days of programming but my dad spent most of his younger days doing it that way and he said that one of the reasons his code has fewer bugs/mistakes than mine (aside from 30+ years exp on me) is because no one wanted to make a stupid mistake somewhere in their stack [of cards] that would mean going to that back of the line just because they used a plus instead of a minus or something (I don't remember the exact example).

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

#15
post #5

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.

[deleted]

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

#16
post #9

I would like to post on there but it's protected from people with low reputation... Maybe someone here appreciates my solution in C: #include int main(int argc, char* argv[]){ int arr[1]; int a = 2; int b = 2; arr[1] = 3; printf("%d", a+b); return 0; } 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

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.

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

#17
post #9

I would like to post on there but it's protected from people with low reputation... Maybe someone here appreciates my solution in C: #include int main(int argc, char* argv[]){ int arr[1]; int a = 2; int b = 2; arr[1] = 3; printf("%d", a+b); return 0; } 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

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.

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

#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 about evil optimizing compilers, it was really hard to make this work.)

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

#19
Because I can't post on stackoverflow either, here's my variant in C (requires glibc, and a deprecated function at that).

  #include 
  #include 
  #include 

  int d_cb(FILE *stream, const struct printf_info *info, const void *const* args) {
    int num = (*(int*)(((int**)args)[0])), numw = info->width;
    char str[10], numn = num|numw, *out = str;
    num = num 
Post reply on HN