This is a collection of algorithms whose objective is to compute pi (π) to high precision. I aim to further expand this list with some other algorithms in the future.
This method is based on randomness. The idea is to generate 2D points randomly in a square with an inscribed circle in it. Let
This method works for up to 4 decimal places. After that, it becomes slow and inaccurate. There are much better methods listed here.
Compile the program with:
g++ montecarlo.cpp -o montecarlo
This approach is based on the Madhava-Leibniz convergent series:
It's able to compute π up to 9 decimal places in about 10 seconds. This is already a pretty good result and it goes beyond any practical use.
Compile the program with:
g++ madhava_leibniz.cpp -o madhava_leibniz
This is the best algorithm listed here. It was discovered in 1985 by Jonathan and Peter Borwein. It's an iterative approach that is able to give over a trillion correct digits after only 20 iterations. The given code computes the first million digits in about a second.
Compile the program with:
gcc borweins.c -o borweins -lmpfr