Pi Day Fun

For Pi Day we take a short diversion to investigate how the famous number Pi is calculated.

March 15, 2023

In celebration of Pi Day yesterday, we decided on a little change of pace from our normal work and spent a few moments calculating the famous irrational number Pi. For this project, we wanted to compare two algorithms for calculating Pi: the Leibniz Series and the Nilakantha Series.

Leibniz Series

Starting with the Leibniz series, we quickly ran out of patience waiting for longer and longer run times to get better and better accuracy. We tried different numbers of iterations to see how the result converged towards the actual value of pi. However, our patience ran out after only 8 digits of accuracy. Not enough!

Nilakantha Series

It was time to see what difference a faster algorithm would make. Enter the Nilakantha Series! This series converges to the actual value of pi much faster. Again, we tried out different numbers of iterations to see how it progressed. We also experimented with changing the number of digits in the code to see how that might affect the accuracy. The result was that higher precision resulted in slower run times but not much more accuracy in the result.

The Verdict

After running both series for the same number of iterations, we ended with eight digits of accuracy for the Leibniz series and 27 digits of accuracy for the Nilakantha series. Nothing like the 1 million digits of pi on PiDay.org or the 100 trillion digits done by Google! But we think not bad at all for a short little diversion on Pi Day.

If you want to see our two short examples, you see them on our GitHub repository. If you have suggestions for improvements, we accept pull requests.