Happy Pi day!

Calculating Pi according to Leibniz’s formula

In honor of International Pi day, which is celebrated on March 14th, I’d like to implement Leibniz’s formula in a code to find the value of Pi (or at least try to).
So, here’s the formula:

 

Basically, it’s an infinite convergence series which can be simplified to this alternating series:

Now, please agree with me when I’m saying that Pi is actually equals 4 – (4/3) + (4/5) – (4/7) + …
So all we need to do is to start subtracting and adding alternating fractions with an odd number as their denominator.
We need to start with the Supremum number which is 4 and to keep in mind that we have to start with subtraction.

 

Aaaaaaand…. voila, we did it!
The number is converging to the precise value of Pi!

Checkout the source code on GitHub.
Here’s a demonstration video.

Have a great day 🙂

More from our blog

Skip to content