Live data from Hacker News

Sum of 1 to 1000000000 in different programming languages

stackoverflow.com

51–60 of 83 posts

Re: Sum of 1 to 1000000000 in different programming languages

#51

Someone replied the following in there: "The key in this case is using C99's long long data type. It provides the biggest primitive storage C can manage (128-bits on 32-bit machines and 256-bits on a 64-bit machine) and it runs really, really fast." Isn't long long "typically" 64-bit? (I know the C standard doesn't actually specify any actual size). What platform does this long long type really give you the full 128…

If you want a guaranteed 64-bit type, put in your code: #include then, use uint64_t for unsigned and int64_t for signed. If you want 128 bits, in gcc you can use __uint128_t (it has two extra underscores at the beginning because that size is nonstandard), but I don't think there is support for 256 bit integers. Try a big integer library: http://stackoverflow.com/questions/124332/c-handling-very-la...

int64_t is just a typdef'd long long in linux and osx:

typedef long long int int64_t;

Re: Sum of 1 to 1000000000 in different programming languages

#53
I think I might be doing it wrong, because I didn't do any looping. I'm a bit too lazy and impatient for that, who wants to spend their afternoon adding all those numbers up even with a computer.

  [joe24pack@staropramen ~]$ python
  Python 2.6.6 (r266:84292, May  1 2012, 13:52:17) 
  [GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2
  Type "help", "copyright", "credits" or "license" for more information.
  >>> def gauss(x):
  ...   return (x+1)*(x/2)
  ... 
  >>> gauss(10)
  55
  >>> gauss(1000000000)
  500000000500000000
  >>>

Re: Sum of 1 to 1000000000 in different programming languages

#54
So nobody does it?

  $sum = int($max/2) * ($max+1));
  $sum += int(($max+1)/2) if ($max & 1);
This is perl of course but it is exploiting the fact that the sum of integers is a sum of constants ($max + 1) with an additional term (the 'middle' integer) if the top number is odd.

Re: Sum of 1 to 1000000000 in different programming languages

#55
post #16

Earlier quoted context omitted.

32-bit or 64-bit won't matter for Node.js. The Number type in JS is specifically defined as using the 64-bit floating point format as defined by IEEE 754, except that all NaNs are coerced to a single value. In terms of the abstraction, there is never a cast when the value overflows; it should just always be considered a double. Under the hood, there may be differences in how the number is actually being treated.

I've heard this before and never understood why. Why?

> Why [+/- 52 bit] ?

Floating point numbers ("floats") work like the scientific notation (e.g. "12 * 10^-3" ). They have an mantissa and an exponent. Double precision (64 bit) floats have a 52 bits integer mantissa, a sign bit, and 11 bits used to represent the exponent, or to encode NaN values ("Not a Number"), like the result of "0/0".

> Why [only floats in JavaScript] ?

The language was supposed to have a low barrier of entry, and having a single number type was thought to be less complex to explain, even though floats have some weird corner cases regarding rounding when you don't understand how they are implemented. Beyond the loss of precision in large numbers, some rationals that have a finite representation in base 10 have an infinite representation in base 2.

For example, 0.2 in base 10 is 0.00110011... in base 2. It is thus rounded to 52 bits. Who says rounding says rounding error.

Here's a Node.JS session that demonstrate the behaviour:

    > e = 0.2
    0.2
    > e = e + 0.2
    0.4
    > e = e + 0.2
    0.6000000000000001
    > e == 0.6
    false
    > e = e + 0.2
    0.8
    > e = e + 0.2
    1
    > e == 1
    true

Re: Sum of 1 to 1000000000 in different programming languages

#56

I think I might be doing it wrong, because I didn't do any looping. I'm a bit too lazy and impatient for that, who wants to spend their afternoon adding all those numbers up even with a computer. [joe24pack@staropramen ~]$ python Python 2.6.6 (r266:84292, May 1 2012, 13:52:17) [GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> def gauss(x): ...…

Hmmmm, make that:

    def gauss(x):
        return (x+1)*x/2
(consider gauss(11), for example)

You gotta admit, on an article about the difference between integer and floating-point arithmetic, that's pretty ironic!

Re: Sum of 1 to 1000000000 in different programming languages

#57
Python 2.7 (Mid 2012 Macbook Pro, 2.5 GHz i5, 8GB, not using SSD)

sum + xrange (consumes ~20MB virtual memory):

    $ time python2.7 -c "print sum(xrange(1,1000000001))"
    500000000500000000
    python2.7 -c "print sum(xrange(1,1000000001))"  11.06s user 0.02s system 99% cpu 11.089 total
reduce + xrange (consumes ~20MB virtual memory):

    $ time python2.7 -c "print reduce(lambda a, b: a + b, xrange(1,1000000001))"
    500000000500000000
    python2.7 -c "print reduce(lambda a, b: a + b, xrange(1,1000000001))"  128.74s user 0.13s system 94% cpu 2:16.51 total
My machine swapping like crazy for more than an hour when I try using range(). I suspect it hasn't even finished allocating the list when I kill the process after it consumes >30GB virtual memory.

    $ time python2.7 -c "print sum(range(1,1000000001))"

Re: Sum of 1 to 1000000000 in different programming languages

#59

I asked a similar question for R about a year ago, and saw an interesting way to do it, taking advantage of the math. http://stackoverflow.com/questions/11623865/faster-modulo-or...

Fun fact: in R, sum(1:1E07) will throw a warning; you have to use sum(as.numeric(1:1E07)) instead, which will indeed give the correct answer.

Re: Sum of 1 to 1000000000 in different programming languages

#60
post #56

I think I might be doing it wrong, because I didn't do any looping. I'm a bit too lazy and impatient for that, who wants to spend their afternoon adding all those numbers up even with a computer. [joe24pack@staropramen ~]$ python Python 2.6.6 (r266:84292, May 1 2012, 13:52:17) [GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> def gauss(x): ...…

Hmmmm, make that: def gauss(x): return (x+1)*x/2 (consider gauss(11), for example) You gotta admit, on an article about the difference between integer and floating-point arithmetic, that's pretty ironic!

It's odd that the odd numbers slipped my mind. Thank you for your gracious correction.
Post reply on HN