Lecture No. 2
One of the most elegant applications of calculus is computing π from scratch. This lecture builds up from the Taylor series for arctan to a practical algorithm that produces hundreds of correct digits — the same method used in the accompanying Python script.
01
Recall that the Taylor series for a function \(f(x)\) centered at 0 sums derivatives at the origin. For \(\arctan(x)\), working out the derivatives and substituting gives the Gregory series — valid for \(|x| \leq 1\).
02
Since \(\arctan(1) = \frac{\pi}{4}\), plugging in \(x = 1\) gives the Leibniz formula. It's beautiful — but practically useless for computation. The series converges so slowly that you need millions of terms for just a handful of correct digits.
03
In 1706, John Machin observed this identity, verifiable via the arctan addition formula. The key insight: both arguments are small fractions. Since each term in the arctan series is \(x^{2n+1}/(2n+1)\), small \(x\) means the series shrinks very rapidly — giving fast convergence instead of the glacial Leibniz pace.
04
For the Leibniz formula, the \(n\)-th term is \(1/(2n+1)\). To get error below \(10^{-d}\) you need roughly \(10^d / 2\) terms.
For Machin's formula, the dominant term shrinks by a factor of \(1/25\) each step, adding roughly \(\log_{10}(25) \approx 1.4\) new correct digits per iteration. For 314 digits we need around 224 terms — a dramatic improvement.
05
The slider controls how many terms of Machin's formula are used. Watch how the approximation homes in on π, and compare the error to the naive Leibniz series at the same term count.
06
To go beyond floating-point precision (about 15 digits), we use Python's decimal module, which supports arbitrary-precision arithmetic. The algorithm is identical — we swap in Decimal types and set getcontext().prec high enough.
The loop runs until the current term drops below \(10^{-(d+10)}\), where \(d\) is the target digit count. Those extra 10 guard digits prevent rounding errors from creeping into the final result. Running with precision = 314 produces all 314 digits of π correctly.
Summary
The Taylor series for arctan gives us a direct route to π via \(\arctan(1) = \pi/4\), but naive evaluation converges too slowly to be useful. Machin's formula sidesteps this by decomposing \(\pi/4\) into arctan evaluations at small arguments, where the series converges geometrically. With arbitrary-precision arithmetic, a few hundred terms is all it takes to compute π to hundreds of digits.