Live data from Hacker News

Sum of 1 to 1000000000 in different programming languages

stackoverflow.com

71–80 of 83 posts

Re: Sum of 1 to 1000000000 in different programming languages

#71
3 milliseconds, man, the C optimizers blow my mind, they basically just cheat and stick the answer in there. :-)

[C]cat sum.c #include int main(void) { unsigned long long sum = 0, i; for (i = 0; i [C]time ./a.out 500000000500000000

real 0m0.003s user 0m0.001s sys 0m0.001s [C]gcc -O3 -S sum.c [C]cat sum.s .section __TEXT,__text,regular,pure_instructions .globl _main .align 4, 0x90 _main: Leh_func_begin1: pushq %rbp Ltmp0: movq %rsp, %rbp Ltmp1: leaq L_.str(%rip), %rdi movabsq $500000000500000000, %rsi xorb %al, %al callq _printf xorl %eax, %eax popq %rbp ret

Re: Sum of 1 to 1000000000 in different programming languages

#72
post #15

Earlier quoted context omitted.

Did you intend to demonstrate a very common off-by-one error in Python?

Probably should've used xrange as well. In the 2.x series, Python's range function returns an actual list. I believe in 3.x range has been replaced with xrange.

I was assuming Python 3, but yes, you are correct.

Re: Sum of 1 to 1000000000 in different programming languages

#73
post #14

Earlier quoted context omitted.

Python: sum(range(1000000000)) :)

It's not right. You should add one to the bound because "range" uses half-open intervals. Also, it only works in Python 3; in Python 2, it creates a list of a 1000000000 integers, so you should use "xrange" instead.

Yep, my bad. Didn't really think before I posted. And yes, I was assuming Python 3, so use xrange on Python 2.

Re: Sum of 1 to 1000000000 in different programming languages

#74
post #9

Earlier quoted context omitted.

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

I think demonstrating a set of features like this between a large number of langauges: say 20 to 50 would be a grand demonstration.

That's what http://www.rosettacode.org is for.

I agree that stackoverflow.com sometimes is a little too trigger-happy when it comes to closing questions, but we really don't need a community-wiki top answer with 1845 upvotes that is basically a rosetta code page, spawing dozens of copy-cat questions with slight variations on the theme.

Re: Sum of 1 to 1000000000 in different programming languages

#75
post #24
post #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).

sum is fine in GHC. It is specialised for Integer. GHCi uses naive sum though.

I tried sum in GHC and it ate up my 16 gigs of ram and then crashed. I'm a noob so maybe I did something wrong, but my code was:

    main = do
        putStrLn $ show  $ sum [1..1000000000]

Re: Sum of 1 to 1000000000 in different programming languages

#76

Clojure: (reduce + (range 1000000001)) Written that way, though, it takes a long time (108 sec, compared to 2.6 sec in C). So that's not fast, as elegant as it may be. This is what I have for C. It's probably not great C code. int main() { long res = 0; int i; for (i = 0; i Faster than (naive?) C is this Clojure loop (no range object). user=> (time (loop [i 0 res 0] (if (> i 1000000000) res (recur (inc i) (+ res i)))…

In Julia:

    julia> @time sum(1:1000000000)
    elapsed time: 1.4527e-5 seconds (88 bytes allocated)
    500000000500000000
@time is a macro that prints the code's execution time and memory usage. This is inside a VM running Linux.

Re: Sum of 1 to 1000000000 in different programming languages

#77

Python is gold

And Python's integer type is gold-plated.

Does that make pypy platinum?

time pypy -c "print sum(xrange(1000000001))"

is 2.0 seconds on my Mac; a C program with 8-byte ints (long or long long) takes 2.6 seconds.

Re: Sum of 1 to 1000000000 in different programming languages

#78
post #75
post #24

Earlier quoted context omitted.

sum is fine in GHC. It is specialised for Integer. GHCi uses naive sum though.

I tried sum in GHC and it ate up my 16 gigs of ram and then crashed. I'm a noob so maybe I did something wrong, but my code was: main = do putStrLn $ show $ sum [1..1000000000]

Pass GHC the -O2 option to turn on optimizations. You need the strictness analyzer to run so that it can determine that sum is strict, otherwise you get a space leak.

Re: Sum of 1 to 1000000000 in different programming languages

#79
post #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).

GHC's strictness analyzer will recognize that sum is strict for Integer and optimize the laziness away. It will only cause a space leak when optimizations are turned off.

Re: Sum of 1 to 1000000000 in different programming languages

#80
post #75

Earlier quoted context omitted.

I tried sum in GHC and it ate up my 16 gigs of ram and then crashed. I'm a noob so maybe I did something wrong, but my code was: main = do putStrLn $ show $ sum [1..1000000000]

Pass GHC the -O2 option to turn on optimizations. You need the strictness analyzer to run so that it can determine that sum is strict, otherwise you get a space leak.

Thank you :)
Post reply on HN