Live data from Hacker News

Write a program that makes 2 + 2 = 5

codegolf.stackexchange.com

51–60 of 76 posts

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

#51
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.

The compiler is probably aligning all stack elements to 8 bytes.

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

#54
post #30

why not just overload the + operator in C++?

first thing i thought when i read the title:

    #include 

    class Int
    {
    private:
            int _value;

    public:
	    Int(int value) : _value(value) {}
	    Int operator + (const Int& b){ return Int(++_value + b._value); }
	    friend std::ostream& operator 
output: 5

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

#55
post #32

Earlier quoted context omitted.

5 bytes: 1|2+2

I think all these bytes are not creative. !![]+2+2 ? !''|2+2 ? 1|2+2 ? Why the use of new operators? Why not just write 1+2+2 ?

Well if you made a general add function that always performed an or operation on the least significant bit, it would be pretty interesting. You'd always flip odd and even in any addition.

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

#56

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 also leads to some interesting programming errors in Java.

    class A {
        public static void main(String[] args) {
            Integer a = 100, b = 100;
            Integer c = 10000, d = 10000;
            System.out.println(a == b);
            System.out.println(c == d);
        }
    }
Prints:

    true
    false
For those unaware, the == check above is doing an Object check (are they the same object). The cache makes the '100' return the same instance of an object. To do actual value checks, you'd need to do c.equals(d)

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

#57
post #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.

http://en.wikipedia.org/wiki/Digraphs_and_trigraphs#C

sorry for spoiling:

"??/" got replaced by "\"

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

#59
post #56

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 also leads to some interesting programming errors in Java. class A { public static void main(String[] args) { Integer a = 100, b = 100; Integer c = 10000, d = 10000; System.out.println(a == b); System.out.println(c == d); } } Prints: true false For those unaware, the == check above is doing an Object check (are they the same object). The cache makes the '100' return the same instance of an object. To do actual val…

what a dumb language! operator overloading should have bound the equals method to the == operator without the programmer having to be aware of this. when in java do you ever want to compare if two objects are the same referenced with the same ptr? Never, so why not do the right thing by the user of the language.

At least be consistent between primitives and objectives.

Sadly, I have to use this language daily. My heart belongs to python, but java pays the bills.

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

#60
post #32

Earlier quoted context omitted.

5 bytes: 1|2+2

I think all these bytes are not creative. !![]+2+2 ? !''|2+2 ? 1|2+2 ? Why the use of new operators? Why not just write 1+2+2 ?

To make many more examples of why Javascript is not a language for beginners.
Post reply on HN