# Chudnovsky algorithm

The Chudnovsky algorithm is a fast method for calculating the digits of π, based on Ramanujan's π formulae. Published by the Chudnovsky brothers in 1988, it was used to calculate π to a billion decimal places.
It 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, 50 trillion digits on January 29, 2020, 62.8 trillion digits on August 14, 2021, 100 trillion digits on March 21, 2022, 105 trillion digits on March 14, 2024, and 202 trillion digits on June 28, 2024.

## Algorithm

The algorithm is based on the negated Heegner number $d=-163$, the j-function $j\left({\tfrac {1+i{\sqrt {-163}}}{2}}\right)=-640320^{3}$, and on the following rapidly convergent generalized hypergeometric series:${\frac {1}{\pi }}=12\sum _{k=0}^{\infty }{\frac {(-1)^{k}(6k)!(545140134k+13591409)}{(3k)!(k!)^{3}(640320)^{3k+3/2}}}$A detailed proof of this formula can be found here:
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 $O\left(n(\log n)^{3}\right)$.

## Optimizations

The optimization technique used for the world record computations is called binary splitting.

### Binary splitting

A factor of 1/640320$^{3/2}${\textstyle 1/{640320^{3/2}}} can be taken out of the sum and simplified to${\frac {1}{\pi }}={\frac {1}{426880{\sqrt {10005}}}}\sum _{k=0}^{\infty }{\frac {(-1)^{k}(6k)!(545140134k+13591409)}{(3k)!(k!)^{3}(640320)^{3k}}}$
Let $f(n)={\frac {(-1)^{n}(6n)!}{(3n)!(n!)^{3}(640320)^{3n}}}$, and substitute that into the sum.${\frac {1}{\pi }}={\frac {1}{426880{\sqrt {10005}}}}\sum _{k=0}^{\infty }{f(k)\cdot (545140134k+13591409)}$
${\frac {f(n)}{f(n-1)}}$ can be simplified to ${\frac {-(6n-1)(2n-1)(6n-5)}{10939058860032000n^{3}}}$, so$f(n)=f(n-1)\cdot {\frac {-(6n-1)(2n-1)(6n-5)}{10939058860032000n^{3}}}$
$f(0)=1$ from the original definition of $f$, so$f(n)=\prod _{j=1}^{n}{\frac {-(6j-1)(2j-1)(6j-5)}{10939058860032000j^{3}}}$
This definition of $f$ is not defined for $n=0$, so compute the first term of the sum and use the new definition of $f$${\frac {1}{\pi }}={\frac {1}{426880{\sqrt {10005}}}}{\Bigg13591409+\sum _{k=1}^{\infty }{{\Bigg\prod _{j=1}^{k}{\frac {-(6j-1)(2j-1)(6j-5)}{10939058860032000j^{3}}}{\Bigg )}\cdot (545140134k+13591409)}{\Bigg )}$
Let $P(a,b)=\prod _{j=a}^{b-1}{-(6j-1)(2j-1)(6j-5)}$ and $Q(a,b)=\prod _{j=a}^{b-1}{10939058860032000j^{3}}$, so${\frac {1}{\pi }}={\frac {1}{426880{\sqrt {10005}}}}{\Bigg13591409+\sum _{k=1}^{\infty }{{\frac {P(1,k+1)}{Q(1,k+1)}}\cdot (545140134k+13591409)}{\Bigg )}$
Let $S(a,b)=\sum _{k=a}^{b-1}{{\frac {P(a,k+1)}{Q(a,k+1)}}\cdot (545140134k+13591409)}$ and $R(a,b)=Q(a,b)\cdot S(a,b)$$\pi ={\frac {426880{\sqrt {10005}}}{13591409+S(1,\infty )}}$
$S(1,\infty )$ can never be computed, so instead compute $S(1,n)$ and as $n$ approaches $\infty$, the $\pi$ approximation will get better.$\pi =\lim _{n\to \infty }{\frac {426880{\sqrt {10005}}}{13591409+S(1,n)}}$
From the original definition of $R$, $S(a,b)={\frac {R(a,b)}{Q(a,b)}}$$\pi =\lim _{n\to \infty }{\frac {426880{\sqrt {10005}}\cdot Q(1,n)}{13591409Q(1,n)+R(1,n)}}$

#### Recursively computing the functions

Consider a value $m$ such that $a<m<b$
* $P(a,b)=P(a,m)\cdot P(m,b)$
* $Q(a,b)=Q(a,m)\cdot Q(m,b)$
* $S(a,b)=S(a,m)+{\frac {P(a,m)}{Q(a,m)}}S(m,b)$
* $R(a,b)=Q(m,b)R(a,m)+P(a,m)R(m,b)$

#### Base case for recursion

Consider $b=a+1$
* $P(a,a+1)=-(6a-1)(2a-1)(6a-5)$
* $Q(a,a+1)=10939058860032000a^{3}$
* $S(a,a+1)={\frac {P(a,a+1)}{Q(a,a+1)}}\cdot (545140134a+13591409)$
* $R(a,a+1)=P(a,a+1)\cdot (545140134a+13591409)$