TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Write a program that makes 2 + 2 = 5

123 pointsby bryan_rasmussenalmost 11 years ago

26 comments

Kenjialmost 11 years ago
I would like to post on there but it&#x27;s protected from people with low reputation... Maybe someone here appreciates my solution in C:<p><pre><code> #include &lt;stdio.h&gt; int main(int argc, char* argv[]){ int arr[1]; int a = 2; int b = 2; arr[1] = 3; printf(&quot;%d&quot;, 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&#x27;s why b is likely to get overwritten with 3 and hence a+b=5
评论 #7829287 未加载
Clexalmost 11 years ago
Here&#x27;s mine :<p><pre><code> $ cat test.c #include &lt;stdio.h&gt; int main() { int a = 3; int b = 3; &#x2F;&#x2F; aren&#x27;t we supposed to add 2 and 2 ??&#x2F; a = 2; b = 2; printf(&quot;%d\n&quot;, a + b); return 0; } $ gcc -W -Wall -trigraphs test2.c 2&gt;&#x2F;dev&#x2F;null $ .&#x2F;a.out 5 $</code></pre>
评论 #7829946 未加载
评论 #7829870 未加载
userbinatoralmost 11 years ago
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&#x27;d heard of an &quot;integer cache&quot; somewhere else&quot; in a WTF-eliciting context... <a href="http://thedailywtf.com/Articles/The-Integer-Cache.aspx" rel="nofollow">http:&#x2F;&#x2F;thedailywtf.com&#x2F;Articles&#x2F;The-Integer-Cache.aspx</a> !
评论 #7830378 未加载
评论 #7830209 未加载
评论 #7829109 未加载
评论 #7829202 未加载
评论 #7829048 未加载
Lockalalmost 11 years ago
UB abuse in C&#x2F;C++ Linux x86-64 (works with gcc&#x2F;clang&#x2F;icc with any optimization level):<p><pre><code> #include &lt;stdio.h&gt; int main() { double x; printf(&quot;Input any number:\n&gt; &quot;); if (scanf(&quot;%lf&quot;, &amp;x) == 1) { printf(&quot;Is 2 + 2 really equals %g? Let&#x27;s try!\n&quot;, x); printf(&quot;2 + 2 = %g\n&quot;, 2 + 2); } else { printf(&quot;Invalid input!\n&quot;); } } </code></pre> Output:<p><pre><code> Input any number: &gt; 5 Is 2 + 2 really equals 5? Let&#x27;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&#x27;s how GCC explorer works): <a href="http://goo.gl/mY9phE" rel="nofollow">http:&#x2F;&#x2F;goo.gl&#x2F;mY9phE</a>
yiedyiealmost 11 years ago
JavaScript ;)<p><a href="http://codegolf.stackexchange.com/a/29944/12328" rel="nofollow">http:&#x2F;&#x2F;codegolf.stackexchange.com&#x2F;a&#x2F;29944&#x2F;12328</a>
评论 #7829194 未加载
rkowalickalmost 11 years ago
Here&#x27;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 #=&gt; 5</code></pre>
评论 #7830124 未加载
评论 #7829478 未加载
评论 #7829215 未加载
评论 #7829425 未加载
im3w1lalmost 11 years ago
<p><pre><code> public class Main { public static void main (String [] args) { System.out.println(&quot;‮5=2+2‭&quot;); &#x2F;&#x2F; prints ‬4=2+2 } }</code></pre>
comexalmost 11 years ago
C:<p><pre><code> #include &lt;stdio.h&gt; #include &lt;stdbool.h&gt; #include &lt;stdlib.h&gt; int nums[5] = {1, 0, 1, 0, 1}; static int cur; int main(int argc, char **argv) { for(int i = 1; nums[i] != 5 &amp;&amp; i &lt; 10; i++) { cur = i; if(i&#x2F;4 == 1) { printf(&quot;2 + 2 &lt;= %d = %s\n&quot;, cur, 2 + 2 &lt;= i ? &quot;true&quot; : &quot;false&quot;); printf(&quot;2 + 2 == %d = %s\n&quot;, cur, 2 + 2 == i ? &quot;true&quot; : &quot;false&quot;); printf(&quot;\n&quot;); } } } </code></pre> No strange array writes! No indirect writes at all!<p>It&#x27;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.)
评论 #7829770 未加载
dglalmost 11 years ago
Because I can&#x27;t post on stackoverflow either, here&#x27;s my variant in C (requires glibc, and a deprecated function at that).<p><pre><code> #include &lt;printf.h&gt; #include &lt;stdio.h&gt; #include &lt;string.h&gt; int d_cb(FILE *stream, const struct printf_info *info, const void *const* args) { int num = (*(int*)(((int**)args)[0])), numw = info-&gt;width; char str[10], numn = num|numw, *out = str; num = num &lt; 0 ? *out++ = &#x27;-&#x27;, -num : numn; do { *out++ = &#x27;0&#x27; + num % 10; num &#x2F;= 10; } while(num); fwrite(str, out - str, 1, stream); return 0; } void init() { register_printf_function(&#x27;d&#x27;, d_cb, 0); } int main() { init(); printf(&quot;2+4 = %d, 2+2 = %1d\n&quot;, 2+4, 2+2); return 0; }</code></pre>
masakalmost 11 years ago
Here&#x27;s the Perl 6 solution:<p><pre><code> multi infix:&lt;+&gt;(2, 2) { 5 } say 2 + 2; # 5</code></pre>
charles2013almost 11 years ago
Ruby solution (don&#x27;t have enough rep to post to codegolf):<p><pre><code> def mad_addr(a,b) a == b &amp;&amp; a == 2 ? (a.object_id + b.object_id)&#x2F;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&#x27;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 `=`]
评论 #7831014 未加载
X4almost 11 years ago
Asking: Isn&#x27;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.
评论 #7829816 未加载
anon4almost 11 years ago
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 &quot;load 4 in register X&quot; instruction to &quot;load 5&quot;, 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&#x27;s 4.
评论 #7829255 未加载
whoopdedoalmost 11 years ago
Lua:<p><pre><code> local debug = require &quot;debug&quot; -- in case 5.2 debug.setmetatable(1, {__tostring=function(n) return (&quot;%g&quot;):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&#x27;s a fallback not an override.
Tooalmost 11 years ago
How about the opposite? Make 2+3=4, here in C.<p><pre><code> add2And3(int *a, int *b) { *a = 3; *b = 2; printf(&quot;%d&quot;, *a + *b); &#x2F;&#x2F;4 } void main() { int storage; add2And3(&amp;storage, &amp;storage); }</code></pre>
isivaxaalmost 11 years ago
<p><pre><code> #!&#x2F;bin&#x2F;bash tty if [[ $? -ne 0 ]] ; then # command returned nonzero echo &quot;not a tty&quot; else # command returned zero n=$? ((n=$n+2)) ((n=$n+2)) echo &quot;0+2+2=$n&quot;; fi</code></pre>
Datsunderealmost 11 years ago
Haskell:<p><pre><code> let 2 + 2 = 5</code></pre>
social_monadalmost 11 years ago
Simple solution in R:<p><pre><code> &gt; &#x27;%+%&#x27; &lt;- get( &#x27;+&#x27;) &gt; &#x27;+&#x27; &lt;- function( e1, e2) 1 %+% e1 %+% e2 &gt; 2+2 [1] 5 </code></pre> Regards
howemanalmost 11 years ago
Combining a couple ideas, here&#x27;s a Go solution<p><a href="http://play.golang.org/p/XnPglbkLW4" rel="nofollow">http:&#x2F;&#x2F;play.golang.org&#x2F;p&#x2F;XnPglbkLW4</a>
评论 #7831599 未加载
gdewildealmost 11 years ago
You have to add 1 to either the first or the second 2 or to the sum of the two.
terraneraalmost 11 years ago
#lang racket (define (add-bizzare x y) (round (+ (+ x 0.4) (+ y 0.4))))
diegoalmost 11 years ago
My first thought in Clojure.<p><pre><code> (let [+ (partial + 1)] (+ 2 2))</code></pre>
NaNaNalmost 11 years ago
The JavaScript answer from @user1886419 is a great joke. lol
评论 #7829703 未加载
runarbergalmost 11 years ago
Julia<p><pre><code> !(a::Int) = !bool(a) !0 + 2 + 2 ##&gt; 5</code></pre>
评论 #7830411 未加载
san86almost 11 years ago
why not just overload the + operator in C++?
评论 #7830320 未加载
nooberminalmost 11 years ago
I&#x27;m surprised no one has suggested this yet. In python<p>&gt;Write a program that seemingly adds the numbers 2 and 2<p>a=2+2<p>&gt;and outputs 5.<p>print(5)<p>Yeah, I know that isn&#x27;t the intent here, but, following the wording of the contest rules exactly...