36. Empirical Pi

You’ve finally snuck into the evil mastermind’s billiards room. You’re only one door away from his office and the Big Red Button that stops the moon laser from vaporizing the Great Barrier Reef.

What is the value of \(\pi\)?”, beeps the security panel.

“Huh. Easy”, you think as you type \(2.71\).

Access denied.

“Mhm. Maybe it needs more digits?” You type \(2.71828\).

Access denied. One attempt remaining.

You feel a cold sweat coming on as you scan the room for something to use. You could try to calculate \(\pi\) with a series expansion, but you don’t trust yourself anymore. No, with one chance to go, you want something foolproof. Billiards table. Chairs. Dartboard. Liqueur cabinet. Dartboard!

Calculating pi with a dartboard
Calculating pi with a dartboard

A dartboard is just a square with an inscribed circle. If the side of the square is \(L\), the radius of the circle is \(\frac{1}{2} L\). So, the ratio of the surfaces of the circle and the square are:

\[ \frac{S_C}{S_S} = \frac{\pi * (\frac{1}{2} L)^2}{L^2} = \frac{1}{4}\pi \]

But you can also find that ratio by throwing darts randomly and counting how many land in the circle. A simple program to do this is here:


from __future__ import print_function
import math, random

acErr = 1e-5

def inCircle(x, y):
    """inCircle(x, y) iff (x, y) is in the circle of radius 1 centered at
    return (math.sqrt(x**2 + y**2) - 1.0) <= acErr

def piGen():
    """Generates increasingly accurate values for Pi"""
    S_circle, S_square = 0.0, 0.0
    while True:
        x, y = random.random(), random.random()
        S_circle += (1 if inCircle(x, y) else 0)
        S_square += 1
        yield (4*S_circle/S_square)

def run_pi():
    print("Pi is 0.00000", end="")
    i = 0
    for p in piGen():
        i += 1
        if i % 25 == 0:
            print("\rPi is %1.5f" % p, end="")

if __name__ == "__main__":

Alternatively, you could use the Gregory-Leibniz series which is easy to remember but much harder to derive:

\[ \frac{\pi}{4} = 1 - \frac{1}{3} + \frac{1}{5} - \frac{1}{7} + \cdots \]