Calculating Pi Digits Up to Infinity Without Using a Library in Python

An explanation of how to calculate pi digits up to infinity with pure math in Python.


Calculating Pi Digits Up to Infinity Without Using a Library in Python

In mathematics, pi is a number that stands for the ratio of any circle's circumference to its diameter. The symbol for pi is π and it is an irrational number, meaning it can't be expressed as a fraction. Additionally, pi is an infinite number, meaning it never ends and never settles into a permanent repeating pattern.

Fortunately for Python programmers, calculating an infinitely long decimal for pi is possible without having to use a library. This tutorial will feature an explanation of how to calculate pi digits up to infinity with pure math in Python.

Setting Up the Calculation

The starting point of calculation Pi in Python is the well-known Leibniz formula:

π / 4 = 1 - 1/3 + 1/5 - 1/7 + ...

This formula was discovered by the German mathematician Gottfried Leibniz and it was the first infinite series to be discovered that could be used to calculate Pi.

To calculate pi in Python, we will use the Leibniz formula until the terms become negligible or until a certain number of digits have been calculated. This process can be implemented in Python with the following code:

pi = 0
sign = 1

for i in range(1,i_max,2):
    pi += sign*(1.0/i) 
    sign *= -1

pi *= 4
print('Calculated value of pi: %.15f' % pi)

Calculating Pi with Higher Precision

The Leibniz formula is a great starting point for calculating pi in Python, but for higher precision it can be improved. To get more accuracy, we can add more terms to the series and the resulting formula looks like this:

π / 4 = 1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + 1/13 + ...

This formula can be implemented in Python with the following code:

pi = 0
sign = 1

for i in range(1,i_max,4):
    pi += sign*(1.0/i) 
    sign *= -1

for i in range(3,i_max,4):
    pi += sign*(1.0/i)
    sign *= -1

pi *= 4
print('Calculated value of pi: %.15f' % pi)

The code above will yield a more accurate calculation of pi, but it is still limited to a finite number of decimal places. To calculate pi to infinity, we need to adjust the formula a bit and add another term to the series:

π/4 = 1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + 1/13 - 1/15 + 1/17 + ... + 1/n

Note: The added term is given by the variable n.

The adjusted formula can be implemented in Python with the following code:

pi = 0
sign = 1

for i in range(1,i_max,2):
    pi += sign*(1.0/i) 
    sign *= -1

pi *= 4
pi += 1/n
print('Calculated value of pi: %.15f' % pi)

Now, the calculated value of pi will be accurate up to any number of digits we specify for the variable n.

Conclusion

In this tutorial, we have learned how to calculate pi digits up to infinity without using a library in Python. We have seen how the Leibniz formula can be used to calculate pi in Python and how the formula can be improved to get higher precision. Additionally, we have learned how to calculate pi to infinity by adding another term to the series.