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.
Write a program that makes 2 + 2 = 5
51–60 of 76 posts
Re: Write a program that makes 2 + 2 = 5
#52 let 2 + 2 = 5Re: Write a program that makes 2 + 2 = 5
#53Re: Write a program that makes 2 + 2 = 5
#54why not just overload the + operator in C++?
#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: 5Re: Write a program that makes 2 + 2 = 5
#55Earlier 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 ?
Re: Write a program that makes 2 + 2 = 5
#56That 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 !
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
#57Here'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
#58Julia !(a::Int) = !bool(a) !0 + 2 + 2 ##> 5
julia> +(a::Int, b::Int) = if a==b==2 5 else a-(-b) end
julia> 2+2
5
julia> 3+1
4
(The else part can't use + or it will cause infinite recursion).Re: Write a program that makes 2 + 2 = 5
#59That 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…
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.