pi day 2026

┌┰────────────────────────────────────────────────┰┐
│┃  3 is within 5% of pi and that's close enough  ┃│
┕┻━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┻┙

it's traditional to celebrate pi day by calculating pi so this year
i thought it'd be interesting to see how much pi i could calculate
on my commodore 64. i chose the leibniz sequence as my approximation
of choice since it's easy to implement but it doesn't converge
very fast so i'm not expecting to get great results.

first, some math

just to briefly show how this sequence works, consider this triangle:

An image of a right isoceles triangle

if you remember your high school trigonometry then looking at
the angle ∠ABC you'll see that we can write down
its tangent (opposite over adjacent):

tan(π/4) = r / r = 1

and we therefore know the inverse:

arctan(1) = π/4

the leibniz sequence gives us a series expansion of the arctangent
function so that's how we'll calculate pi.

here's the formula leibniz discovered (from Wikipedia):

arctan(x) = the sum from k=0 to infinity of ((-1)^k(x^(2k+1)))/(2k + 1)

we know arctan(1) is π/4 so we can plug in 1 to this formula
to find the value of pi to whatever precision we want

so that's what i made my commodore 64 do all day:
calculate the terms of this sequence, add them all up,
and multiply by 4 at the end to get an approximation of pi

the program

i spent a few hours on pi day eve writing a BASIC program that could
compute pi from the first n terms of the leibniz sequence and after
doing my best to optimize it i benchmarked its performance at about
30 milliseconds per term - i.e. very, very, very slow.

correction: i wrote the algorithm from memory and later realized
i actually implemented Machin's formula, not the Leibniz formula,
but the two are very similar and use the same basic approach;
see Machin-like formula on wikipedia for more info

for comparison, the python version of this program running on my
6-year-old desktop can compute 200,000 terms in that amount of time.
part of that is due to commodore BASIC being much slower than machine
code - up to 100x slower in some benchmarks - but it still goes to
show how much faster computers have gotten in the last 40 years.

anyway, at that speed i estimated that if i left it running for all
24 hours of pi day then it would be able to compute the first
2,600,000 terms of the sequence. one day later, here's what i got:

A photo of the C64's monitor showing pi = 3.14159704

so there you go, pi = 3.14159704

note that the duration displayed on the C64 is wrong;
i think because the timer rolled over after running for so long

all in all a pretty good result considering how little time i spent
on this project and how old the hardware is. next year i'll probably
try a different approximation or maybe different hardware altogether
- the C64 keyboard really is a pain to use.

links

* Leibniz formula for pi
* Machin's Formula
* video demonstration of the program i wrote

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
written by peter beard on 2026-03-14, last updated 2026-03-16
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -