HACKER Q&A
📣 eddtries

How do you calculate Pi to N digits?


How do you actually calculate pi to say 1000 digits? or 10,000? Most info I found on google take you to pre-solved lists of digits OR code that is optimised beyond recognition so I'm unsure how you actually do the calculation to begin with.


  👤 Someone Accepted Answer ✓
https://en.wikipedia.org/wiki/Approximations_of_π

The method of using inscribed and circumscribed n-gons conceptually is the simplest (1), but converges very slowly. See https://arxiv.org/pdf/2008.07995.pdf.

(1) at least to non-mathematicians. Pure mathematicians will want to prove the claims that the circumference of a circle is larger than/smaller than that of its inscribed/circumscribed polygon and that both can get arbitrarily close to it if the number of sides gets large enough.


👤 Leftium
Matt Parker makes a new video on calculating Pi (by hand) every year: https://www.youtube.com/playlist?list=PLhtC92GarkjyYbxI3-4qz...

The more practical methods:

- Chudnovsky algorithm: https://youtu.be/LhlqCJjbEa0

- Newton's method: https://youtu.be/CKl1B8y4qXw?t=160


👤 FrankWilhoit
Search "infinite series for pi". Several sources contain historical surveys of series: the earliest-discovered converge slowly, the best recent ones I think quadruple the number of digits on each iteration.

👤 Bostonian
There was a paper

https://maa.org/sites/default/files/pdf/pubs/amm_supplements... A Spigot Algorithm for the Digits of π Author(s): Stanley Rabinowitz and Stan Wagon Source: The American Mathematical Monthly, Vol. 102, No. 3 (Mar., 1995), pp. 195-203

Fortran code for the algorithm is at https://fortran-lang.discourse.group/t/fortran-code-snippets...


👤 westurner
To approximate an arbitrary number of digits of pi with sympy:

  #! pip install sympy gmpy
  import sympy
  sympy.evalf(sympy.pi)
  sympy.N(sympy.pi)
https://docs.sympy.org/latest/modules/evalf.html

RosettaCode > [Python] lists quite a few more: https://rosettacode.org/wiki/Pi#Python


👤 h2odragon
https://www.bellard.org/pi/

dig into the links a bit