Skip to content

Technikal Mind

Technical thoughts from a Curious Thinker

  • Home
  • About
  • GitHub
  • Curious Thinker Media

Computationally Calculating Pi [in python]

Posted on March 16, 2016 - May 27, 2022 by technik

In honor of Pi day (3/14) and my increased fascination with mathematics as a whole, I decided to look under the hood and understand how the value of pi is calculated to be 3.14159… and write a program to do it.

Introducing: Pi-Py. Keep reading to learn more about the underlying process!

As it turns out, there are a number of approaches mathematicians have taken. For now, I chose to work with two methods: the Gregory-Leibniz series and the Nilakantha series. Here’s how they work.


Gregory-Leibniz

This method can accurately calculate \pi to 5 decimal places after approximately 450,000 iterations. That’s a lot!

Using sigma notation:

    \[ \sum\limits_{i=0}^\infty \frac{(-1)^n}{2n+1} = \frac{\pi}{4} \]

This is basically fancy notation for a loop – a summation.

In this case, i=0 is the initial value, \infty is the number of iterations, and \frac{(-1)^n}{2n+1} is the body of the loop.

Another way of representing this, which is perhaps easier to understand more directly, is as follows:

    \[ 4 - \frac{4}{3} + \frac{4}{5} - \frac{4}{7} + \frac{4}{9} - ... = \pi \]

(Note: I multiplied each number by 4 to get the value of \pi rather than \frac{\pi}{4})

This can be easily computed in a loop by alternating addition and subtraction of odd numbers. View the code on GitHub to see how this was implemented!


Nilakantha

This method is considerably faster – by about 10,000 times, as a matter of fact. After only 45 iterations, \pi&s=0&bg=00090a&fg=ffffff can be calculated to the same 5 decimal places as the Gregory-Leibniz method.

In the 15th Century, Indian astronomer/mathematician Nilakantha Somayaji published the following infinite series:

    \[ 3 + \frac{4}{2\times3\times4} - \frac{4}{4\times5\times6} + \frac{4}{6\times7\times8} - \frac{4}{8\times9\times10} + ... = \pi \]

As the denominator value changes incrementally, this too can be easily computed using a simple loop.

View the code on GitHub to see how I implemented the Nilakantha infinite series!

The Nilakantha method converges at a much faster rate than the Gregory-Leibniz method, but there are even faster series which I have not yet studied or attempted to implement as life has kept me busy! Nevertheless, it has been an interesting journey, and I’ll be posting about other projects soon.

As a side-note, I had the chance to learn some LaTeX syntax as a result of writing this article, which really helped to make the material look presentable.

Posted in Computer Science, Mathematics, Programming

Post navigation

Contribution to an open source project
Building a Media Player using Vlc.DotNet, vlclib and WPF

Recent Posts

  • Building a Media Player using Vlc.DotNet, vlclib and WPF
  • Computationally Calculating Pi [in python]
  • Contribution to an open source project
  • Starting Computer Science program
  • Unity Game Development

Recent Comments

    Archives

    • September 2016
    • March 2016
    • November 2015
    • August 2015
    • July 2015
    • June 2015
    • April 2015
    • February 2015
    • January 2015
    • November 2014
    • August 2014
    • May 2013

    Categories

    • C#/.NET
    • Computer Science
    • Development
    • Education
    • Electronics
    • GameDev
    • Linux
    • Mathematics
    • Networking
    • Philosophy
    • Programming
    • Science
    • Technikal Mind
    • Uncategorized

    Meta

    • Log in
    • Entries feed
    • Comments feed
    • WordPress.org
    Proudly powered by WordPress | Theme: micro, developed by DevriX.