Live data from Hacker News

A Million Digits of Pi in 9 Lines of JavaScript

ajennings.net

31–40 of 86 posts

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

#31

Can someone explain the logic behind the evolution of the fractions at each stage? I see that (1/4) becomes (1/4^2) and (1/4^3), but it's not obvious to me how (1/3)->(1/5)->(1/7) flows (odds? primes?), or (1/2) -> (13/24) -> (135/246). EDIT: I understand now, the numerator on the first term is ascending odds and the denominator is ascending evens. Thanks for everyone's help!

If you look at the Taylor series link, you can see the sum representations of trigonometric functions.

So for instance, the 3/5/7 is just the (2n+1) value.

The other looks like the sum of the product of (2n-1)/(2n) for values 1 to n.

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

#32
post #30

Earlier quoted context omitted.

Is that simplified? I'm just going off of this image: http://ajennings.net/blog/images/formula.png

me too, the pattern to me is odds on the numerator and evens in the denominator, and the second fraction goes 1/3, 1/5, 1/7, 1/9... i think we're saying the same thing i misunderstood your comment. 1357/2468 is the first fraction of the NEXT line that isnt in the image

Aha, yes, I edited my post now to say 4th line not 4th step. Leave it to HN to get an off-by-one error :)

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

#33

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)"

Yes, 4 * arctan(1) will do in any language. I just find some joy in knowing a formula that uses only addition, multiplication, and addition, and computing it directly.

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

#34

Can someone explain the logic behind the evolution of the fractions at each stage? I see that (1/4) becomes (1/4^2) and (1/4^3), but it's not obvious to me how (1/3)->(1/5)->(1/7) flows (odds? primes?), or (1/2) -> (13/24) -> (135/246). EDIT: I understand now, the numerator on the first term is ascending odds and the denominator is ascending evens. Thanks for everyone's help!

odds, not primes.

And I'm sorry if it looks like 13 over 24. It's supposed to look like 1/2 times 3/4. (The next term adds 5/6, and so on.)

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

#36
post #16

This also takes a lot of RAM. Keep an eye on it during longer executions or the swapping could make your box pretty unusable.

It should take 415kB to store a million digit number, and the algorithm only needs to keep two of them. So, 1MB total to calculate a million digits of pi. I wonder if there are a lot of temporary allocations that build up until they get garbage collected.

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

#37
post #9

For those of us behind work proxies that dumbly think this site is "domain parking" or worse: http://archive.is/hUo6Q

Personal domain. I haven't used it for much over the years.

The hosting is static pages on S3.

I wonder if there's anything I could do to avoid the domain getting flagged.

Maybe it's because the root page on the domain is just a quote.

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

#38
post #16

This also takes a lot of RAM. Keep an eye on it during longer executions or the swapping could make your box pretty unusable.

I used to have a script on my personal site that would just continue to compute digits of Pi until your browser crashed. I finally took it down after too many recruiters and potential employers keep clicking on the link that said, "Don't Click This." and complaining about it.

I did the same thing when I was testing it. I would keep an eye on the system RAM resources graph as the script was running. Watching the RAM start to spike was oddly satisfying.

It's pretty scary to me how easy it is to crash a browser these days with something so simple.

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

#39
post #16

This also takes a lot of RAM. Keep an eye on it during longer executions or the swapping could make your box pretty unusable.

It should take 415kB to store a million digit number, and the algorithm only needs to keep two of them. So, 1MB total to calculate a million digits of pi. I wonder if there are a lot of temporary allocations that build up until they get garbage collected.

Not sure what it is exactly, but forcing a garbage collection (using the "minimize memory usage" button in about:memory) doesn't make any difference.

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

#40

What is the actual equation? They just list the first couple of terms.

Continuing the pattern it's

  $\pi = 3 + \sum_{k=1}^\infty 3 \frac{(2k-1)!!}{(2k)!!} \frac{1}{2k+1} \frac{1}{4^k}$ [1].
n!! is double factorial, the product of odd or even numbers up to n (depending on whether n is odd or even) [2].

Edit: added a simpler series from https://math.stackexchange.com/a/14116:

  $\pi = \sum_{k=0}^{\infty} \frac{(2k)!!}{(2k+1)!!} \left(\frac{1}{2}\right)^{k-1}$
[1] https://imgur.com/a/YtA8kUx

[2] https://en.wikipedia.org/wiki/Double_factorial

Post reply on HN