Here’s a simple algorithm that Yoshiaki Tamura and Yasumasa Kanada used to calculate π to 16 million places. It’s based on Gauss’ study of the arithmetic-geometric mean of two numbers. “Instead of using an infinite sum or product, the calculation goes round and round in a loop,” writes David Wells in The Penguin Dictionary of Curious and Interesting Numbers. “It has the amazing property that the number of correct digits approximately doubles with each circuit of the loop.” Start with these values:
Then follow these instructions:
The last instruction prints the first approximation to π; then you loop up to the top and run through the instructions again.
Running through the loop just three times gives an approximation to π that’s already correct to 5 decimal places:
Loop 1: 2.9142135
Loop 2: 3.1405797
Loop 3: 3.1415928
And running the loop a mere 19 times gives π correct to more than 1 million decimal places.