Program that adds all natural numbers and yields -1/12 As you might know there is a mathematical fun-fact that if you add all natural numbers you end up with... -1/12 (see Wikipedia here).

Of course this is very strange result and can not be obtained by just adding one number followed by another, but some special mathematical tricks.

However your task is to write a program, that looks like it attempts to add all natural numbers, but when you run it - it returns -1/12.

In pseudocode it might look like this:

result  = 0;
counter = 1;
while(true) {
  result  += counter;
  counter ++;
}
println(result);


You can do this in any way you like - you can exploit some buffer overflow, play with errors thrown while some variable becomes too big or just hide the crucial thing along the code in some clever way. The only conditions are that code should at first look as if it attempts to add all natural numbers and when run it returns -1/12 (in any format, it might be decimal, binary, text, ascii art whatever).

The code can of course contain much more, than shown above, but it should be clear enough, to fool reader.

This is popularity contest - vote for the most clever idea!
C

Should work on platforms where both sizeof(float) and sizeof(int) are 4 and follows the IEEE floating point standard (I guess).

Version 1:



#define toFloat(x) (*(float*)&amp;x)
#define ABS(x)     (x&lt;0 ? (-x) : x)
#include &lt;stdio.h&gt;
int main() {
    unsigned int sum=0;
    int i=1;
    /* Since we really can't sum to infinity,
     * we sum it until it is very close to -1/12, within 3 decimal places.
     * Need to convert sum to float since -1/12 is not int                 */
    while(!(ABS(toFloat(sum) + 1./12) &lt;= 0.001)) {
        sum+=i;
        i++;
    }
    printf("%.3f\n", toFloat(sum));
    return 0;
}


Output: -0.083

Explanation:


   Not a very interesting answer, but with misleading comments.  The sum from 1 to 79774 is 3181985425, which has the same binary representation as -0.082638867199420928955078125 when interpreted as a float instead of an unsigned int.  Note that !(abs&lt;=0.001) is used instead of abs&gt;0.001 to avoid quitting the loop when the sum reaches 2139135936 (NaN in float). (Thanks to @CodesInChaos for suggesting this idea instead of an independent isNaN check.)


Special thanks to @Geobits for the idea of terminating the loop by comparing the sum instead of the counter.

Edit: Version 2

#include &lt;stdio.h&gt;
const float inf = 1./0.;
int main() {
    int x=1;
    int sum=0xBDAAAAAB; // Arbitrary magic number for debugging
    while(x --&gt; inf) { // while x tends to infinity (?)
        sum+=x;
    }
    float sumf=*(float*)&amp;sum; // convert to float since -1/12 is not int
    if(sumf == 0xBDAAAAAB) { // no sum performed, something's wrong with the loop...
        fprintf(stderr, "sum is unchanged\n");
        return -1;
    }
    printf("%f\n", sumf);
    return 0;
}


Output: -0.083333

Explanation:


   Uses the same int-to-float trick, but with the --&gt; "tends to" operator here. Since every number is smaller than infinity the loop will not be executed even once.  After converting to float it is compared with the int magic number (i.e. -0.83333 is compared with 0xBDAAAAAB, or 3182078635), which of course is different.