Live data from Hacker News

A Million Digits of Pi in 9 Lines of JavaScript

ajennings.net

41–50 of 86 posts

Re: A Million Digits of Pi in 9 Lines of JavaScript

#41
His demo page, in my Chrome, if I enter 10000, it takes about 2 seconds to finish with 10k digits.

But if I enter 100k, it takes 30 seconds to get to reporting 10k digits worth of progress.

Hmm. Have to think about that one. Just cause it's asking JS to do comparisons of much larger numbers?

Re: A Million Digits of Pi in 9 Lines of JavaScript

#42
post #13

> this simple one still converges at about 0.6 decimal digits per term. Quick proof of this: as the number of terms n in the sum goes to infinity, the ratio of each term to the previous one is approximately 1/4 - the first factor contributes m/(m+1) , the second q/(q+2) for some m and q that go to infinity along with n , the third contributes 1/4. If we counted base 4, then the value of each digit would be on average…

I wonder if base 4 math is how people recite this out loud.

Re: A Million Digits of Pi in 9 Lines of JavaScript

#43

His demo page, in my Chrome, if I enter 10000, it takes about 2 seconds to finish with 10k digits. But if I enter 100k, it takes 30 seconds to get to reporting 10k digits worth of progress. Hmm. Have to think about that one. Just cause it's asking JS to do comparisons of much larger numbers?

I'd bet a good portion of the difference is between rendering the additional digits as it goes.

Re: A Million Digits of Pi in 9 Lines of JavaScript

#44
post #18

On any standard unix system with bc installed - it's preinstalled on most of them, you can calculate pi to $n digits using bc: bc -l <<< "scale=$n; 4*a(1)"

The algorithm seems to be at least quadratic in the length. On a 2014 i7 Mac mini, (n, time(sec)) = (1000, 0.29), (2000, 1.65), (4000, 9.70), (8000, 58.42).

I think this section on Wikipedia is relevant: https://en.wikipedia.org/wiki/Approximations_of_%CF%80#Grego...

Re: A Million Digits of Pi in 9 Lines of JavaScript

#45

His demo page, in my Chrome, if I enter 10000, it takes about 2 seconds to finish with 10k digits. But if I enter 100k, it takes 30 seconds to get to reporting 10k digits worth of progress. Hmm. Have to think about that one. Just cause it's asking JS to do comparisons of much larger numbers?

I'd bet a good portion of the difference is between rendering the additional digits as it goes.

What do you mean, doesn't it do that either way?

Re: A Million Digits of Pi in 9 Lines of JavaScript

#46

His demo page, in my Chrome, if I enter 10000, it takes about 2 seconds to finish with 10k digits. But if I enter 100k, it takes 30 seconds to get to reporting 10k digits worth of progress. Hmm. Have to think about that one. Just cause it's asking JS to do comparisons of much larger numbers?

It's not just the comparisons, it's the additions, multiplications and divisions too. When you enter 10,000, each iteration of the loop is working with numbers 10,000 digits long. When you enter 100,000, each iteration is working with numbers 100,000 digits long, which I imagine makes every operation slower.

Re: A Million Digits of Pi in 9 Lines of JavaScript

#47

His demo page, in my Chrome, if I enter 10000, it takes about 2 seconds to finish with 10k digits. But if I enter 100k, it takes 30 seconds to get to reporting 10k digits worth of progress. Hmm. Have to think about that one. Just cause it's asking JS to do comparisons of much larger numbers?

The script uses BigInts to implement (sort of) fixed point arithmetic. So with 100k as input, each operation works at a much higher precision.

Re: A Million Digits of Pi in 9 Lines of JavaScript

#48

His demo page, in my Chrome, if I enter 10000, it takes about 2 seconds to finish with 10k digits. But if I enter 100k, it takes 30 seconds to get to reporting 10k digits worth of progress. Hmm. Have to think about that one. Just cause it's asking JS to do comparisons of much larger numbers?

Any numeric representation bigger than your CPU register will not have a fixed operation cost.

Re: A Million Digits of Pi in 9 Lines of JavaScript

#50

Earlier quoted context omitted.

I'd bet a good portion of the difference is between rendering the additional digits as it goes.

What do you mean, doesn't it do that either way?

Sure but one has 10x more digits than the other so takes a heck of a lot longer to convert and display the string for the same number of iterations.

As a quick test of my theory the majority of the time is being spent trying to display the progress: - Default 100,000 = 58.276 - CSS display: none; = 22.359 - Display when done = 20.057

Post reply on HN