Hacker News new | past | comments | ask | show | jobs | submit login

Yes, in theory it could print all the numbers you said, but in practice it won't because of floats dropping precision at some point. For JavaScript that point is 2^53.

The easiest way to see this is by typing `9007199254740992 + 1` in your browser's dev console. It should spit out `9007199254740992`. `(Math.pow(2, 53) + 1) == Math.pow(2, 53)` returns true.

That means your program would print invalid output (or rather start looping infinitely) starting at that number (which is much smaller than 2^63) thus disqualifying your solution.




When the person I was responding to said the largest JS int is 2^54-1 I assumed that meant I could use all integers in [0, 2^54-1], but you are right that I can only go up 2^53. That breaks the specific code I posted but not the underlying idea. Fixing it to deal with that lower upper bound is easy.

Just change the constant 10000000000000000 in the outer loop to 1000000000000000 (which is 2^49+437050046578688 and well below 2^53), change the filling of the right[] array to

  for (let r = 0; r < 10; ++r) {
    right.push('000' + itos(r) + '\n')
  }   
  for (let r = 10; r < 100; ++r) {
    right.push('00' + itos(r) + '\n')
  }   
  for (let r = 100; r < 1000; ++r) {
    right.push('0' + itos(r) + '\n')
  }   
  for (let r = 1000; r < 10000; ++r) {
    right.push(itos(r) + '\n')
  }
and change the loop that starts "for (let r = 1; r < 1000; ++r) {" to go to 10000 instead of 1000.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: