How to Calculate the Digits of Pi with Python
by aoum, Mar 2, 2025, 1:26 AM
How to Calculate the Digits of Pi
Pi (π) is one of the most famous irrational numbers, representing the ratio of a circle's circumference to its diameter. Over the centuries, mathematicians have developed various methods to calculate its digits. In this blog post, we'll explore a few methods used to approximate π.
1. Leibniz Formula
One of the earliest methods to calculate π is the Leibniz formula. This formula is simple but converges very slowly, meaning it requires many terms to get a decent approximation of π.
The formula is:

In general, the nth term is:

By summing these terms, we can approximate π. However, the series converges very slowly, so it would take millions of terms to get a few accurate digits of π.
Click to see Python code for Leibniz formula
2. Archimedes' Method
The ancient Greek mathematician Archimedes developed a method for approximating π by inscribing and circumscribing polygons around a circle. The more sides the polygons have, the closer their perimeter gets to the circumference of the circle, allowing for a more accurate estimate of π.
Archimedes began with a hexagon and doubled the number of sides of the polygon in each step. As the number of sides increased, the polygon better approximated the circle.
For example, with a 96-sided polygon, Archimedes was able to estimate that π was between 3.1408 and 3.1429.
Click to see Python code for Archimedes' method
3. Gauss-Legendre Algorithm
The Gauss-Legendre algorithm is much faster than the Leibniz formula. This iterative algorithm converges to π very quickly, providing a highly accurate result with fewer iterations.
Here’s how the algorithm works:
Initialize:

For each iteration, update:

After several iterations, the approximation for π is:

This method converges very quickly, so even a few iterations will give you a very accurate estimate of π.
Click to see Python code for Gauss-Legendre algorithm
4. Chudnovsky Algorithm
For high precision calculations of π, the Chudnovsky algorithm is one of the fastest algorithms known. This formula converges incredibly fast, which is why it’s used to calculate billions of digits of π.
The formula is:

The terms in this series rapidly get smaller, so after just a few terms, you can get a highly accurate approximation of π.
Click to see Python code for Chudnovsky algorithm
5. Using Built-in Libraries
If you don't need a custom solution and just want a quick approximation, many programming languages offer built-in libraries for π. For instance, in Python, the math module provides an easy way to access the value of π.
Example:
Many programming languages, such as Python, C++, and Java, provide built-in constants for π, allowing you to avoid manually calculating the value. These built-in constants use the precision of the underlying machine to give you an accurate approximation of π.
Conclusion
There are many different methods to calculate the digits of π, ranging from simple, slow formulas like the Leibniz series to highly efficient algorithms like the Chudnovsky algorithm. The method you choose will depend on how many digits you need and the level of precision you require.
For those looking to calculate a few digits quickly, built-in libraries or simpler formulas will suffice. But for those interested in pushing the limits of precision, algorithms like Gauss-Legendre and Chudnovsky are excellent choices!
Feel free to explore the various methods we've discussed to approximate π, and try to calculate it to as many digits as possible using the methods described. Each method has its pros and cons depending on the use case and computational resources available.
Go ahead and try out the methods described in this post and explore the different techniques for calculating π. Happy learning!
Pi (π) is one of the most famous irrational numbers, representing the ratio of a circle's circumference to its diameter. Over the centuries, mathematicians have developed various methods to calculate its digits. In this blog post, we'll explore a few methods used to approximate π.
1. Leibniz Formula
One of the earliest methods to calculate π is the Leibniz formula. This formula is simple but converges very slowly, meaning it requires many terms to get a decent approximation of π.
The formula is:

In general, the nth term is:

By summing these terms, we can approximate π. However, the series converges very slowly, so it would take millions of terms to get a few accurate digits of π.
Click to see Python code for Leibniz formula
def leibniz_pi(n_terms):
pi = 0
for i in range(n_terms):
pi += ((-1) ** i) / (2 * i + 1)
return 4 * pi
# Example usage:
n_terms = 1000000
approx_pi = leibniz_pi(n_terms)
print(approx_pi)
2. Archimedes' Method
The ancient Greek mathematician Archimedes developed a method for approximating π by inscribing and circumscribing polygons around a circle. The more sides the polygons have, the closer their perimeter gets to the circumference of the circle, allowing for a more accurate estimate of π.
Archimedes began with a hexagon and doubled the number of sides of the polygon in each step. As the number of sides increased, the polygon better approximated the circle.
For example, with a 96-sided polygon, Archimedes was able to estimate that π was between 3.1408 and 3.1429.
Click to see Python code for Archimedes' method
import math
def archimedes_pi(n_sides):
side_length = 1
perimeter = n_sides * side_length
while n_sides < 1000: # Increase the number of sides
n_sides *= 2
side_length = math.sqrt(2 - 2 * math.sqrt(1 - (side_length / 2) ** 2))
perimeter = n_sides * side_length
return perimeter / 2
# Example usage:
n_sides = 6 # Start with a hexagon
approx_pi = archimedes_pi(n_sides)
print(approx_pi)
3. Gauss-Legendre Algorithm
The Gauss-Legendre algorithm is much faster than the Leibniz formula. This iterative algorithm converges to π very quickly, providing a highly accurate result with fewer iterations.
Here’s how the algorithm works:
Initialize:

For each iteration, update:

After several iterations, the approximation for π is:

This method converges very quickly, so even a few iterations will give you a very accurate estimate of π.
Click to see Python code for Gauss-Legendre algorithm
import math
def gauss_legendre(n):
a = 1
b = 1 / math.sqrt(2)
t = 1 / 4
p = 1
for _ in range(n):
a_next = (a + b) / 2
b = math.sqrt(a * b)
t -= p * (a - a_next) ** 2
a = a_next
p *= 2
return (a + b) ** 2 / (4 * t)
# Example usage:
iterations = 10
approx_pi = gauss_legendre(iterations)
print(approx_pi)
4. Chudnovsky Algorithm
For high precision calculations of π, the Chudnovsky algorithm is one of the fastest algorithms known. This formula converges incredibly fast, which is why it’s used to calculate billions of digits of π.
The formula is:

The terms in this series rapidly get smaller, so after just a few terms, you can get a highly accurate approximation of π.
Click to see Python code for Chudnovsky algorithm
import math
def chudnovsky_algorithm(n_terms):
C = 426880 * math.sqrt(10005)
M = 1
L = 13591409
X = 1
K = 6
S = L
for k in range(1, n_terms):
M = (K**3 - 16*K) * M // k**3
L += 545140134
X *= -262537412640768000
S += M * L / X
K += 12
return C / S
# Example usage:
n_terms = 5
approx_pi = chudnovsky_algorithm(n_terms)
print(approx_pi)
5. Using Built-in Libraries
If you don't need a custom solution and just want a quick approximation, many programming languages offer built-in libraries for π. For instance, in Python, the math module provides an easy way to access the value of π.
Example:
import math
print(math.pi) # Prints the value of pi up to the precision of floating point numbers
Many programming languages, such as Python, C++, and Java, provide built-in constants for π, allowing you to avoid manually calculating the value. These built-in constants use the precision of the underlying machine to give you an accurate approximation of π.
Conclusion
There are many different methods to calculate the digits of π, ranging from simple, slow formulas like the Leibniz series to highly efficient algorithms like the Chudnovsky algorithm. The method you choose will depend on how many digits you need and the level of precision you require.
For those looking to calculate a few digits quickly, built-in libraries or simpler formulas will suffice. But for those interested in pushing the limits of precision, algorithms like Gauss-Legendre and Chudnovsky are excellent choices!
Feel free to explore the various methods we've discussed to approximate π, and try to calculate it to as many digits as possible using the methods described. Each method has its pros and cons depending on the use case and computational resources available.
Go ahead and try out the methods described in this post and explore the different techniques for calculating π. Happy learning!

This post has been edited 2 times. Last edited by aoum, Mar 2, 2025, 1:34 AM