Estimating Pi using the Basel Problem

The Basel Problem

In the 17th century, mathematicians became fascinated by an infinite series known as the Basel Problem.
The question was simple to state but extremely difficult to solve:

What is the exact value of the following infinite series?

1/1² + 1/2² + 1/3² + 1/4² + ...

This can also be written using sigma notation:
\sum_{n=1}^{\infty}(1 / n²)

Many famous mathematicians attempted to solve it, including members of the Bernoulli family who lived in the Swiss city of Basel (hence the name of the problem). However, no one could determine its exact value.

That changed in 1734, when the brilliant mathematician Leonhard Euler discovered something remarkable.

Euler’s Remarkable Discovery

Euler proved that the infinite series actually equals:

\sum_{n=1}^{\infty}(1 / n²) = π² / 6

This result surprised mathematicians because it revealed a deep connection between:

  • Number theory (the sum of reciprocals of squares)
  • Geometry through the constant Ï€

Rearranging Euler’s formula allows us to express Ï€ in terms of the series:

π = \sqrt{(6 × \sum_{n=1}^{\infty}(1 / n²))}

This means we can estimate π by computing a partial sum of the series.
The more terms we include, the closer we get to the true value of π.

Approximating π Numerically

Since we cannot compute an infinite number of terms, we approximate the series using the first N values:

π ≈ \sqrt{(6 × \sum_{n=1}^{N}(1 / n²))}

As N increases, the approximation becomes more accurate.
This is a perfect opportunity to use Python and an iterative algorithm to calculate an approximate value of Pi using a large number of iterations (N).

Python Challenge

Your challenge is to write a Python program that estimates the value of π using Euler’s solution to the Basel Problem.

Your program should:

    Ask the user how many terms of the series should be calculated.
    Use a loop to calculate the sum of 1 / n².
    Use Euler’s formula to estimate Ï€.
    Display the estimated value of π.
    Display the difference between your estimate and Python’s math.pi.

Example Output:

How many terms should be used? 10000

Estimated value of Pi: 3.14149
Actual value of Pi: 3.141592653589793
Difference: 0.00010

You can now complete the code using the following Python IDE:

Improving the Accuracy

The accuracy depends on the number of terms used.

Terms (N) Approximation of π
10 3.04936
100 3.13208
1,000 3.14064
10,000 3.14149

The true value of π is approximately 3.14159265359, so the estimate improves steadily as N increases.
However, this method converges relatively slowly compared to modern algorithms such as the Gregory-Leibniz series or the Nilakantha series.

Find out more Python challenges based on estimating π using your Python skills:

unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area

Did you like this challenge?

Click on a star to rate it!

Average rating 5 / 5. Vote count: 1

No votes so far! Be the first to rate this post.

As you found this challenge interesting...

Follow us on social media!