Chudnovsky algorithm
The Chudnovsky algorithm is a fast method for calculating the digits of π, based on Ramanujan’s π formulae. It was published by the Chudnovsky brothers in 1988[1] and was used in the world record calculations of 2.7 trillion digits of π in December 2009,[2] 10 trillion digits in October 2011,[3][4] 22.4 trillion digits in November 2016,[5] 31.4 trillion digits in September 2018–January 2019,[6] and 50 trillion digits on January 29, 2020.[7]
The algorithm is based on the negated Heegner number , the j-function , and on the following rapidly convergent generalized hypergeometric series:[2]
A detailed proof of this formula can be found here:[8]
For a high performance iterative implementation, this can be simplified to
There are 3 big integer terms (the multinomial term Mq, the linear term Lq, and the exponential term Xq) that make up the series and π equals the constant C divided by the sum of the series, as below:
- , where:
- ,
- ,
- ,
- .
The terms Mq, Lq, and Xq satisfy the following recurrences and can be computed as such:
The computation of Mq can be further optimized by introducing an additional term Kq as follows:
Note that
- and
This identity is similar to some of Ramanujan's formulas involving π,[2] and is an example of a Ramanujan–Sato series.
The time complexity of the algorithm is .[9]
Programming example
An example implementation of the Chudnovsky algorithm, written in Python 3, is provided below.
#!/usr/bin/env python3
import decimal
from decimal import Decimal as D # For internal use only.
def pi(precision: int = decimal.getcontext().prec,
rounding: decimal.Context.rounding = decimal.ROUND_FLOOR
) -> decimal.Decimal:
"""Return pi as a decimal.Decimal to the specified precision.
This function uses the Chudnovsky algorithm
(https://wikipedia.org/wiki/Chudnovsky_algorithm) and implements
acceleration by using the Mq (and Kq), Lq, and Xq recurrence
patterns.
Note that the first argument is the precision, NOT the number of
decimal places. The relationship is:
decimal places = precision + 1
Positional/keyword arguments:
precision -- the precision to which pi is calculated
(default decimal.getcontext().prec)
rounding -- how to round the final value of pi
(default decimal.ROUND_FLOOR)
"""
# Add 2 to the precision to ensure the accuracy of the answer.
precision += 2
decimal.getcontext().prec = precision
# Use incremental calculations.
# Set initial values for q, C, L, X, K, and M.
q = D(0)
C = D(426880)*D(10005).sqrt()
L = D(13591409)
X = D(1)
K = D(-6)
M = D(1)
running_sum = D(0)
pi = D('NaN')
# Iteration 0.
lastpi = pi
running_sum += M*L/X
pi = C * running_sum**D(-1)
# Loop until pi and lastpi converge to the current precision.
while pi - lastpi:
lastpi = pi
q += D(1)
L += D(545140134)
X *= D(-262537412640768000)
K += D(12)
M *= (K**D(3) - D(16)*K) / q**D(3)
running_sum += M*L/X
pi = C * running_sum**D(-1)
# Reduce precision back to the specified precision.
precision -= 2
decimal.getcontext().prec = precision
# quantize pi to sepcified precision with specified rounding.
return pi.quantize(D('1e-'+str(precision-1)), rounding)
if __name__ == '__main__':
print(pi()) # Calculate pi to current precision.
print(pi(51)) # Calculate pi to 50 decimal places.
# Expected output (https://oeis.org/A000796):
#
# >>> print(pi())
# 3.141592653589793238462643383
# >>> print(pi(51))
# 3.14159265358979323846264338327950288419716939937510
See also
References
- ^ Chudnovsky, David; Chudnovsky, Gregory (1988), Approximation and complex multiplication according to ramanujan, Ramanujan revisited: proceedings of the centenary conference
- ^ a b c Baruah, Nayandeep Deka; Berndt, Bruce C.; Chan, Heng Huat (2009), "Ramanujan's series for 1/π: a survey", American Mathematical Monthly, 116 (7): 567–587, doi:10.4169/193009709X458555, JSTOR 40391165, MR 2549375
- ^ Yee, Alexander; Kondo, Shigeru (2011), 10 Trillion Digits of Pi: A Case Study of summing Hypergeometric Series to high precision on Multicore Systems, Technical Report, Computer Science Department, University of Illinois, hdl:2142/28348
- ^ Aron, Jacob (March 14, 2012), "Constants clash on pi day", New Scientist
- ^ "22.4 Trillion Digits of Pi". www.numberworld.org.
- ^ "Google Cloud Topples the Pi Record". www.numberworld.org/.
- ^ "The Pi Record Returns to the Personal Computer". www.numberworld.org/.
- ^ Milla, Lorenz (2018), A detailed proof of the Chudnovsky formula with means of basic complex analysis, arXiv:1809.00533
- ^ "y-cruncher - Formulas". www.numberworld.org. Retrieved 2018-02-25.