53
8
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!
3
I'm voting to close this question as off-topic because underhanded challenges are no longer on-topic on this site. http://meta.codegolf.stackexchange.com/a/8326/20469
– cat – 2016-04-18T02:28:06.3402Fixed your tags: if it's popularity-contest it can't be code-golf, and we have an underhanded tag for challenges like "write code that looks like x but does y". Anyway, this is pretty decent challenge for a newcomer! :) – Martin Ender – 2014-05-26T23:57:40.940
2@m.buettner - thanks for editing tags, yes, I am new here, so I am not aware of all tags. I'll try to follow the rules! – Paweł Tokarz – 2014-05-27T00:00:58.740
3Why were all of the answers along with the question just downvoted? Downvoter: please leave a comment. – arshajii – 2014-05-27T02:01:02.057
7
The first line is not entirely true, depending on your interpretation http://math.stackexchange.com/questions/39802/why-does-123-dots-frac112
– qwr – 2014-05-27T05:41:04.943@qwr - yes, that's true, this is not exactly a sum, but something sometimes considered as generalized summation. But yeah, this was only an excuse to funny contest :) – Paweł Tokarz – 2014-05-27T07:38:21.430
2
You should see this numberphile video about the subject - https://www.youtube.com/watch?v=w-I6XTVZXww
– Danko Durbić – 2014-05-27T09:03:46.127I've seen that! That was partially an inspiration... – Paweł Tokarz – 2014-05-27T22:36:22.397