Live data from Hacker News

Sum of 1 to 1000000000 in different programming languages

stackoverflow.com

11–20 of 83 posts

Re: Sum of 1 to 1000000000 in different programming languages

#11
post #3

This 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.

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.

Re: Sum of 1 to 1000000000 in different programming languages

#14

Ruby (1..1000000000).inject(:+)

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.

Re: Sum of 1 to 1000000000 in different programming languages

#15

Earlier quoted context omitted.

Python: sum(range(1000000000)) :)

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.

Re: Sum of 1 to 1000000000 in different programming languages

#16
post #3

This 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.

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?

Re: Sum of 1 to 1000000000 in different programming languages

#17
post #3

This 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.

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.

Yep, there are only doubles in JavaScript. With doubles, integer values work fine up to 2^53. After that point, things get silly.

    >>> Math.pow(2, 53) === Math.pow(2, 53) - 1
    false
    >>> Math.pow(2, 53) === Math.pow(2, 53)
    true
    >>> Math.pow(2, 53) === Math.pow(2, 53) + 1
    true // derp

Re: Sum of 1 to 1000000000 in different programming languages

#18
APL:

  +/⍳1E9
Try it yourself, download NARS2000 (free) here:

http://www.nars2000.org/

The "⍳" character is entered by typing ALT+i

Explanation:

  1E9 is 1,000,000,000
  ⍳1E9 generates a vector containing integers from 1 to 1,000,000,000 (inclusive)
  +/ is the sum of the vector
Any good APL interpreter will not actually generate a vector with a billion numbers but rather recognize the above expression and optimize the resulting operation for speed and minimal resource utilization.

EDIT:

Technically the "/" is the "reduction" operator acting along the last axis. In the case of a single dimensional array it acts along the only available axis. If, instead, it was acting on a matrix it would reduce along the columns. Here's a longer annotated example with output from the interpreter:

  Generate a vector from 1 to 10:
      ⍳10
  1 2 3 4 5 6 7 8 9 10

  Sum:
      +/⍳10
  55

  Generate a vector of ten one's and zero's, repeating until the end:
      10⍴1 0
  1 0 1 0 1 0 1 0 1 0

  Now use that vector to reduce the original 1 to 10 vector, grabbing every other
  element starting with the first.  The effective result is that you end up with 
  all the odd numbers between 1 and 10:
      (10⍴1 0)/⍳10
  1 3 5 7 9

  Same things, now grabbing the even numbers by flipping the 1 0 sequence to 0 1:
      (10⍴0 1)/⍳10
  2 4 6 8 10

  Sum of all odd integers between 1 and 10:
      +/(10⍴1 0)/⍳10
  25

    Sum of all even integers between 1 and 10:
      +/(10⍴0 1)/⍳10
  30

  One could create a single vector with the odd integers between 1 and 10 followed by
  the even integers between 1 and ten by simply concatenating the generating
  expressions (APL executes from right to left):
      ((10⍴1 0)/⍳10),(10⍴0 1)/⍳10
  1 3 5 7 9 2 4 6 8 10

  And then you can reshape ("⍴") the result into a matrix:
      2 5⍴((10⍴1 0)/⍳10),(10⍴0 1)/⍳10
  1 3 5 7  9
  2 4 6 8 10

  Finally, use the scan operator again, now applied to a matrix, to sum along the 
  columns and produce a result for each row.  The effect is to output a two element
  vector with the sum of the odd integers between 1 and 10 as the first element
  and the sum of all the even integers between 1 and 10 as the second:
      +/2 5⍴((10⍴1 0)/⍳10),(10⍴0 1)/⍳10
  25 30
If you want to try this type the lines immediately following my comments above right into the interpreter. The rho "⍴" or reshape operator is entered by typing ALT+r.

Hope this helps make sense of it. Of course, there are other ways to accomplish the same thing.

https://en.wikipedia.org/wiki/APL_syntax_and_symbols

Re: Sum of 1 to 1000000000 in different programming languages

#19
post #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

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

Re: Sum of 1 to 1000000000 in different programming languages

#20
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?

http://www.ecma-international.org/ecma-262/5.1/#sec-4.3.19

As for why? I have no clue. It's specified in the specification. That's all. There are ToInteger(), ToInt32(), ToUint16(), ToUInt32 functions defined as well, but I think that's for the host to implement

EDIT: oops. there isn't a ToInt64() function defined.

Post reply on HN