Estimating π, Slowly

π-squared divided by six is an exceedingly interesting number. Perhaps most famously, it's the sum of the reciprocals of the squares of the natural numbers (1, 2, 3, etc).

112+122+132+=π26 \frac{1}{1^2} + \frac{1}{2^2} + \frac{1}{3^2} + \dots = \frac{\pi^2}{6}

This is a specific case of the Riemann zeta function, a topic of great interest in many branches of mathematics, as well as featuring in one of the notable open problems of the current era (as of this writing).

This number appears in another interesting place: the probability that two randomly chosen large integers are coprime is one in π26\frac{\pi^2}{6}, or in other words, 6π2\frac{6}{\pi^2}, or about 60.8%.

Two numbers being coprime means that they share no common factor other than 1.

Let's choose two random positive integers: 14 and 15.

14=2×715=3×5 \begin{align} 14 &= 2 \times 7\\ 15 &= 3 \times 5 \end{align}

None of those prime factors are common between our two numbers, so we say these are coprime. Choosing two more: 9 and 21.

9=3×321=3×7 \begin{align} 9 &= 3 \times 3\\ 21 &= 3 \times 7 \end{align}

These are not coprime: they share a common factor of 3.

If we repeat that process over and over, we'd find that about three of every five pairs of numbers are coprime.

I find this a fascinating result. I've always associated π more closely with matters of geometry, be it a circle or an oscillating wave. Yet, we find π appearing in what, on its face, seems more of a number theory problem.

While we contemplate the deep connections between fundamental mathematical concepts, let's have some fun with a computer. Computers are exceptionally good at performing the following two tasks:

And so, we shall estimate π by random simulation. Given the non-zero number of random trials T where we choose two random positive integers, and the non-zero number of observed coprime pairs in those trials C, we have:

CT6π26TCπ \begin{align} \frac{C}{T} &\approx \frac{6}{\pi^2} \\ \sqrt{\frac{6T}{C}} &\approx \pi \end{align}

I wrote some generally un-optimized Python to perform our calculations.

I then rented a virtualized CPU core purporting to be an Intel Xeon 8358 and arrived at the following approximations:

Trials Coprime Pairs π Error Duration
100 1 2.449489742783178 22% 28 µs
101 5 3.4641016151377544 10.3% 38 µs
102 64 3.0618621784789726 2.6% 203 µs
103 583 3.208051620104573 2.1% 1.5 ms
104 6,056 3.1476228685452723 0.19% 15 ms
105 60,632 3.1457534252934933 0.13% 148 ms
106 607,766 3.142009000396122 0.0133% 1.5 s
107 6,078,367 3.141826265052723 0.0074% 14.8 s
108 60,790,037 3.141661727139629 0.0022% 2m 29s
109 607,937,818 3.141564964973601 0.0009% 24m 48s

So indeed, we can get a very close approximation given enough time. I wouldn't suggest using this method for our day-to-day calculations, of course. Nobody has time to wait half an hour for a mostly-correct π. I suggest using either the defined constant in your programming language (e.g. math.pi) or perhaps memorizing three or four digits. And if all else fails, there's always 227\frac{22}{7}.

Previous: Relearning Math - The Difference of Squares

All Posts | Back to Home