Ask HN: How do you calculate Pi to N digits?

6 points by eddtries ↗ HN
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.

12 comments

[ 3.0 ms ] story [ 22.7 ms ] thread
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.
(comment deleted)
(comment deleted)
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.

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

I want to try write the underlying code myself, but thanks!
gmpy2.const_pi(precision=0: =53bits) probably isn't too helpful then either: https://gmpy2.readthedocs.io/en/latest/mpfr.html#gmpy2.const... https://github.com/aleaxit/gmpy/issues/253#issue-499509692

RosettaCode has only one solution to the "Approximate Pi with Rationals (not float64s)" in Python problem.

I remember having seen a few statistical manifestation of Pi demos on YouTube