Visualizing Leibniz' Pi series

For millenia, humanity’s best approximation of $\pi$ remained $\frac{22}{7}$, where we had arrived at by literally drawing circles and measuring the ratio of their diameter and circumference. Only a few centuries ago did we arrive at the much more accurate, but still just approximative $\frac{355}{113}$.

There just was no known number closer to Pi back then. We literally did not have a way to find $\pi$ exactly, consequently nobody knew what $\pi$ truly is. Kind of crazy!

The one to change this was the French mathematician Vieta, who first came up with a series that would converge to $\pi$, allowing us to get arbitrarily close to it. That series was pretty complicated, lucky us however, since Leibniz later found a much nicer series:

$$ \frac{4}{1}-\frac{4}{3}+\frac{4}{5}-\frac{4}{7}+\frac{4}{9}\dots $$

In Asimov’s 1960 essay A Piece of Pi, this series is presented much more interestingly than I could ever, however I have one crucial advantage over Asimov. As he writes:

Being a naive nonmathematician myself, with virtually no mathematical insight worth mentioning, I thought, when I first decided to write this essay, that I would use the Leibniz series to dash off a short calculation and show you how it would give x easily to a dozen places or so. However, shortly after beginning, I quit.

You may scorn my lack of perseverance, but any of you are welcome to evaluate the Leibniz series just as far as it is written above, to $\frac{4}{15}$, that is. You can even drop me a postcard and tell me the result, If, when you finish, you are disappointed to find that your answer isn’t as close to x as the value of $\frac{355}{113}$, don’t give up. Just add more terms. [If] any of you finds how many terms it takes to improve on drop me a line and tell me that, too.

Being blessed with a computing device, I won’t need to do any of that grunt work myself. Let’s see if we can answer Asimov’s questions. Here’s Leibniz’ series in Python:

def pi(n):
    result = 0
    for i in range(n):
        result += ((-1) ** i) * 4 / (2 * i + 1)
    return result
>>> pi(1)     # 4.00
>>> pi(10)    # 3.04
>>> pi(100)   # 3.13
>>> pi(101)   # 3.15

How well does $\frac{355}{113}$ fare?

>>> 355/113   # 3.1415929203539825
>>> math.pi   # 3.141592653589793

Oof. Looks like we have some catching up to do. The error for pi(100) is in the range of $10^{-2}$ while that for $\frac{355}{113}$ is in $10^{-7}$.

Five orders of magnitude! And we already have gone through 100 elements of the series. It’s easy to see why Asimov seems salty about this.

In reality, this is how slowly this series converges to $\pi$:

>>> pi(1)         #  4.0
>>> pi(10)        #  3.0418396189294032
>>> pi(100)       #  3.1315929035585537
>>> pi(1000)      #  3.140592653839794
>>> pi(10000)     #  3.1414926535900345
>>> pi(100000)    #  3.1415826535897198
>>> pi(1000000)   #  3.1415916535897743
>>> pi(10000000)  #  3.1415925535897915
>>> pi(100000000) #  3.141592643589326

It appears that for every ten times the iterations we get one more accurate decimal digit. The error, therefore, only becomes smaller than $10^{-7}$ somewhere around $10^7$ iterations.

Why is this series taking so long to get close to its point of convergence? Let’s take a look at it:

def pi(n):
    result = 0
    series = []
    for i in range(n):
        result += ((-1) ** i) * 4 / (2 * i + 1)
        series += [result]
    return result, series
>>> import matplotlib.pyplot as plt
>>> r, s = pi(1000)
>>> plt.plot(s)
>>> plt.show()

Leibniz

To zoom in a bit, we let $n=100$:

Leibniz

It’s simply osciallating between the ends of an interval that grows smaller logarithmically as you increase $n$. Makes perfect sense considering the formula is $\frac{4}{2i+1} = O(\frac{1}{x})$.

With that knowledge we might want to try to make a guess at how many iterations it would have taken to get a smaller error than $\frac{355}{113}$:

>>> abs(math.pi - 355/113)
2.667641894049666e-07

So at least according to Python, the error is $\frac{2.667641894049666}{10000000}$ or about $\frac{1}{3748629}$, which would naturally correspond to $n=3748629$. Let’s try that:

>>> abs(math.pi - pi(3748629)) < abs(math.pi - 355/113)
True
>>> abs(math.pi - pi(3748628)) < abs(math.pi - 355/113)
False

Yess!

In fact here’s how this looks for the numbers surrounding $3748629$:

n Smaller error?
3748624 False
3748625 False
3748626 False
3748627 False
3748628 False
3748629 True
3748630 False
3748631 True
3748632 True
3748633 True

It does indeed seem that $3748629$ is the smallest $n$ for which the error in Leibniz’ series is smaller than that of $\frac{355}{113}$.

Now clearly, this table is not formal proof just yet (it’s only missing a few million entries), but given that the sequence is monotonically decreasing it also wouldn’t be hard to argue formally that this is indeed the smallest $n$.

Anyway, that was just a fun series to mess around with for a bit, and obtain some solutions to Asimov’s questions, whether they were being sarcastic or not. How easily we can find even a brute force answer nowadays when in the 1600s the very smartest minds had merely managed to find $\pi$ to 15 digits is something to think about.