from Hacker News

A Million Digits of Pi in 9 Lines of JavaScript

by ajennings on 9/12/19, 4:37 PM with 81 comments

  • by dmurray on 9/12/19, 7:41 PM

    > 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 1/4 of the previous one, certainly for a normal number like pi. But we count base 10, so we get log_10 4 decimal digits every time we get one base-four digit. Which is very close to 0.6.

  • by jrochkind1 on 9/12/19, 10:16 PM

    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?

  • by tombert on 9/12/19, 6:45 PM

    Wow, I wonder why Firefox is so much slower? Quantum seems pretty zippy overall, but maybe that's largely due to fast rendering speeds?

    Does anyone on the Spidermonkey team have some insight?

  • by mourner on 9/13/19, 6:02 AM

    Alternatively, you can generate Pi digits one by one in a streaming way: https://observablehq.com/@mourner/calculating-pi-digits
  • by wasnthere on 9/12/19, 7:50 PM

    JS Error `No identifiers allowed directly after numeric literal` when running http://ajennings.net/pi.html on Mac OS Mojave 10.14.6, Safari 12.1.2 (14607.3.9)
  • by phonebucket on 9/12/19, 7:32 PM

    A fun related thread on math stackexchange which has several examples of series which converge quickly to pi: https://math.stackexchange.com/questions/14113/series-that-c...

    edit: There also some nice formulae for quick convergence in this article: https://julialang.org/blog/2017/03/piday

  • by AdmiralAsshat on 9/12/19, 8:29 PM

    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!

  • by bakul on 9/13/19, 8:20 AM

    > this simple one still converges at about 0.6 decimal digits per term.

    The Chudnovsky brothers’ algorithm computes 14.18... digits per term. Its implementation in Scheme is only about a couple dozen lines of code. It computes a million pi digits in about 17.5 seconds on raspberry pi 4 in Gambit Scheme (57 seconds on the original raspberry pi, IIRC).

  • by 52-6F-62 on 9/12/19, 7:15 PM

    For those of us behind work proxies that dumbly think this site is "domain parking" or worse:

    http://archive.is/hUo6Q

  • by LeoPanthera on 9/12/19, 7:49 PM

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

  • by russellbeattie on 9/13/19, 2:43 AM

    I was going to make a joke about just writing Math.PI.toFixed(10 * * 6), but it turns out that Number.toFixed() only supports up to 100 places AND Math.PI stops at the 50th place. Still amusing to me. Also, BigInt numbers can't be combined with non BigInt without conversion. I haven't had cause to use them yet, so I didn't know that.

    Learned three new things making a dumb HN joke... not bad!

  • by userbinator on 9/13/19, 2:27 AM

    I thought it would be the "spigot" algorithm, which is based on Bellard's formula (yes, that Bellard) and yields similarly small programs:

    http://numbers.computation.free.fr/Constants/TinyPrograms/ti...

  • by sp332 on 9/12/19, 7:54 PM

    This also takes a lot of RAM. Keep an eye on it during longer executions or the swapping could make your box pretty unusable.
  • by andig on 9/20/19, 3:10 PM

    I still can't work out how the formula shown translates to the code given. Any hints?
  • by adossi on 9/12/19, 5:49 PM

    I'm interested to see how the computation time would progress with the recent V8 memory enhancements.
  • by foxes on 9/13/19, 2:57 AM

      let y=3n*(10n**1000020n);
      const f=(i,x,p)=>{(x>0)?f(i+2n,x*i/((i+1n)*4n),p+x/(i+2n)):p/(10n**20n)} 
      console.log(f(1n,y/8n,y));
    
    Not sure if I can golf it anymore
  • by ilovepeppapig on 9/13/19, 9:22 AM

    It will be much faster if you don't update the HEX values all the time (they are not that interesting anyway).
  • by hervature on 9/12/19, 6:56 PM

    What is the actual equation? They just list the first couple of terms.
  • by dlojudice on 9/12/19, 7:05 PM

    Is there a BigDecimal, Money, or similar coming to JS?
  • by paulpauper on 9/12/19, 8:24 PM

    infinite series ..an amazing discovery
  • by craftinator on 9/13/19, 5:09 AM

    wget "https://www.piday.org/million/"

    Got you beat, js.