Easy Pi

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:

\mathrm{A}=1
\mathrm{X}=1
\mathrm{B}=1/\sqrt{2}
\mathrm{C}=1/4

Then follow these instructions:

\textrm{Let}\:  \mathrm{Y}=\mathrm{A}

\textrm{Let}\:  \mathrm{A}=\displaystyle\frac{\mathrm{A}+\mathrm{B}}{2}

\textrm{Let}\:  \mathrm{B}=\sqrt{\mathrm{BY}}

\textrm{Let}\:  \mathrm{C}=\mathrm{C}-\mathrm{X}(\mathrm{A}-\mathrm{Y})^{2}

\textrm{Let}\:  \mathrm{X}=2\mathrm{X}

\textrm{PRINT}\: \displaystyle\frac{\left ( {\mathrm{A}+\mathrm{B}} \right )^{2}}{{4\mathrm{C}}}

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.