Computing π to two billion digits

A C++ implementation of the Chudnovsky Algorithm - an algorithm used to efficiently compute pi. Using Binary Splitting architecture, GMP, and OpenMP, it has been able to compute nearly 5 million digit in just less than a second. So far, it've computed up to 2,000,000,000 digits (2 billion).

Benchmarks

All runs recorded on a 13th Gen i5-13500H laptop (16 threads), with WSL2.

DigitsBinary splittingPeak memory
1,000,0000.23708466 s29.7 MiB
10,000,0001.98447877 s266.7 MiB
100,000,00027.75211740 s2.29 GiB
200,000,000

How it works

$$\frac{1}{\pi} \;=\; 12 \sum_{k=0}^{\infty} \frac{(-1)^k \, (6k)! \, (13591409 + 545140134\,k)} {(3k)! \, (k!)^3 \, 640320^{\,3k + 3/2}}$$

What makes it fast

How to build

Needs a C++ compiler with OpenMP and the GMP library.

git clone https://github.com/IMBOBTHECODER/pi
cd pi
g++ -O3 -fopenmp main.cpp -o main -lgmp
./main

Limitations & future

Memory is the wall: computing digits is cheap, holding them is not — the two-billion-digit run leaned on 64 GiB of swap.

References

  1. S. Ramanujan — Modular Equations and Approximations to π, Quarterly Journal of Mathematics (1914)
  2. D. V. Chudnovsky & G. V. Chudnovsky — The Computation of Classical Constants, PNAS 86 (1989)
  3. J. M. Borwein & P. B. Borwein — Pi and the AGM (1987)
  4. B. Haible & T. Papanikolaou — Fast Multiprecision Evaluation of Series of Rational Numbers (1997)
  5. The GNU MP manual