Sum of 1 to 1000000000 in different programming languages
stackoverflow.com
Sum of 1 to 1000000000 in different programming languages
1–10 of 83 posts
Re: Sum of 1 to 1000000000 in different programming languages
#2It would have been interesting to see this problem solved in many different languages. But I guess that would kill the question on Stackoverflow.
Re: Sum of 1 to 1000000000 in different programming languages
#3This issue happens on 32 bits builds of PHP and nodejs : The language switches to a floating point representation when the result of some operation exceeds INT_MAX.
In 64 bits PHP builds, the computation is done right.
Re: Sum of 1 to 1000000000 in different programming languages
#4Python is gold
Re: Sum of 1 to 1000000000 in different programming languages
#5Ruby
(1..1000000000).inject(:+)Re: Sum of 1 to 1000000000 in different programming languages
#6Python is gold
And Python's integer type is gold-plated.
Re: Sum of 1 to 1000000000 in different programming languages
#7Ruby (1..1000000000).inject(:+)
Python:
sum(range(1000000000))
:)Re: Sum of 1 to 1000000000 in different programming languages
#8 /*author: Gauss */
var n = 1000000000;
var sum = n*(n+1)/2;Re: Sum of 1 to 1000000000 in different programming languages
#9It would have been interesting to see this problem solved in many different languages. But I guess that would kill the question on Stackoverflow.
I don't think it would be that interesting - and I don't think we need to rediscover the fact that some languages use IEEE754 as the default number type over and over again
Re: Sum of 1 to 1000000000 in different programming languages
#10Haskell
foldl' (+) 0 [1..1000000000]
You could use sum, but that will eat up a lot of RAM because of the laziness.EDIT: For the fun of it, I decided to do the same in a slightly more esoteric language, so here's a Prolog version (given that your stack is big enough)
rangesum(0,0).
rangesum(N,X) :- M is N - 1, rangesum(M,Y), X is Y + N.
?- rangesum(1000000000, X), write(X).