Live data from Hacker News

Sum of 1 to 1000000000 in different programming languages

stackoverflow.com

1–10 of 83 posts

Re: Sum of 1 to 1000000000 in different programming languages

#9
post #2

It 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

#10
Haskell

    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).
Post reply on HN