Chudnovsky algorithm


The Chudnovsky algorithm is a fast method for calculating the digits of pi|, based on Ramanujan’s formulae. It was published by the Chudnovsky brothers in 1988, and was used in the world record calculations of 2.7 trillion digits of in December 2009, 10 trillion digits in October 2011, 22.4 trillion digits in November 2016, 31.4 trillion digits in September 2018–January 2019, and 50 trillion digits on January 29, 2020.
The algorithm is based on the negated Heegner number, the j-function, and on the following rapidly convergent generalized hypergeometric series:
A detailed proof of this formula can be found here:
For a high performance iterative implementation, this can be simplified to
There are 3 big integer terms that make up the series and pi| equals the constant C divided by the sum of the series, as below:
The terms Mk, Lk, and Xk satisfy the following recurrences and can be computed as such:
The computation of Mk can be further optimized by introducing an additional term Kk as follows:
Note that
This identity is similar to some of Ramanujan's formulas involving, and is an example of a Ramanujan–Sato series.
The time complexity of the algorithm is.

Example: Python implementation

can be computed to any precision using the above algorithm in any environment which supports arbitrary-precision arithmetic. As an example, here is a Python implementation:

from decimal import Decimal as Dec, getcontext as gc
def PI: # Parameter defaults chosen to gain 1000+ digits within a few seconds
gc.prec = prec
K, M, L, X, S = 6, 1, 13591409, 1, 13591409
for k in range:
M *= Dec / k**3
L += 545140134
X *= -262537412640768000
S += Dec / X
K += 12
pi = 426880 * Dec.sqrt / S
pi = Dec # Drop few digits of precision for accuracy
print =\n".format)
return pi
Pi = PI
print
print
print