Spock’s π Gambit — The Math Explained

ΣComputing π

From sigma notation to a for loop — the math Spock knew by heart.
A high school tutorial in infinite series and asymptotic convergence

Lesson 1 — Why Bother Computing π?

You already know π. It is the ratio of a circle’s circumference to its diameter. You have been using π ≈ 3.14 since middle school. So why would we want to compute it?

Because π is irrational. It cannot be written as a fraction. Its decimal expansion is infinite and non-repeating:

π = 3.14159265358979323846264338327950288… (forever)

There is no pattern. There is no “last digit.” A computer asked to find the last digit of π will compute forever. This is exactly what Mr. Spock knew when he gave the Enterprise computer that impossible command.

Over the centuries, mathematicians discovered series — carefully constructed infinite sums that converge to π. The series in this tutorial was discovered independently by James Gregory in 1671 and Gottfried Leibniz in 1674. It is the simplest connection between mathematics and code that exists for π.

Lesson 2 — The Gregory-Leibniz Series

Here is the series. Don’t panic — we will take it apart, symbol by symbol:

π  =  4 × Σ k=0 to ∞ (−1)k 2k + 1

Let’s decode each piece:

The Σ Symbol

Σ is the Greek capital letter Sigma. In mathematics it means “add up everything that follows, for all the values of k shown below and above it.”
The k=0 at the bottom means “start with k = 0.”
The at the top means “keep going forever.”

(−1)k — The Alternating Sign

When k = 0: (−1)0 = +1
When k = 1: (−1)1 = −1
When k = 2: (−1)2 = +1
When k = 3: (−1)3 = −1

It alternates: +, −, +, −, +, − … forever. That is the “alternating” series.

2k + 1 — The Odd Numbers

When k = 0: 2(0)+1 = 1
When k = 1: 2(1)+1 = 3
When k = 2: 2(2)+1 = 5
When k = 3: 2(3)+1 = 7

The denominator steps through all the odd numbers: 1, 3, 5, 7, 9, 11 …

Put It Together

The series writes out as:

π = 4 ×   11  −  13  +  15  −  17  +  19  −  …

The first few partial sums:
After 1 term:   4 × 1/1 = 4.000
After 2 terms: 4 × (1 − 1/3) = 2.667
After 3 terms: 4 × (1 − 1/3 + 1/5) = 3.467
After 4 terms: 4 × (1 − 1/3 + 1/5 − 1/7) = 2.895
After 5 terms: … = 3.340

It jumps above and below π, getting closer each time. That is the asymptotic convergence you will see in the graph below.

Lesson 3 — From Σ to Code: The For Loop Connection

Here is the exciting part. Every symbol in the sigma notation maps directly to a line of JavaScript code. Look at them side by side:

Sigma Notation

π = 4 × Σ (k=0 to ∞)   (−1)k 2k + 1
var sum = 0;
for (var k = 0; k < n; k++) {
  var sign = (k % 2 === 0) ? 1 : -1;
  sum += sign / (2 * k + 1);
}
var pi_approx = 4 * sum;

The Exact Mapping

Every symbol in the sigma notation appears in the code:

Math Symbol Meaning Code Equivalent
Σ Sum all the terms for loop + sum +=
k = 0 (lower bound) Start at k = 0 var k = 0 (initialiser)
(upper bound) Go forever (we stop at n) k < n (loop condition)
k++ (implied) Move to next term k++ (increment)
(−1)k Alternating +/− sign (k % 2 === 0) ? 1 : -1
2k + 1 k-th odd number 2 * k + 1
4 × Multiply sum by 4 4 * sum
Key insight: The sigma symbol Σ is not mysterious. It is simply a for loop written in mathematical notation. When you see Σ in any formula, you are looking at a loop. The expression to the right of Σ is the loop body. The bounds below and above tell you the starting and stopping value of the loop variable.

Lesson 4 — The Interactive Graph

Now you can see the convergence. Drag the slider to increase the number of terms. Watch what happens to the blue line:

  • With very few terms the line zigzags wildly above and below π.
  • With more terms the zigzag shrinks as the approximation gets closer and closer.
  • The gold dashed line is the true value of π — the destination the series is approaching but never reaching.
Terms: 10
The Numbers
Terms (n):
Series approx:
True π:
Absolute error:
Error bound (4/2n+1):
Why does it converge so slowly? The terms of the Leibniz series are 1, 1/3, 1/5, 1/7 … They decrease slowly. The error after n terms is approximately 4/(2n+1). After 1,000 terms the error is still about 0.002 — only 3 decimal places. For practical computation, faster series like the Machin formula (1706) or the Chudnovsky algorithm (1988) converge dramatically faster, but they are far harder to explain. The Leibniz series earns its place in teaching precisely because of its simplicity.

Lesson 5 — Discussion Questions

Questions to Consider
  1. The alternating series. After each term, the approximation jumps above then below π. Why does adding the next term always bring you closer, never further away? (Hint: look at how quickly the terms shrink.)
  2. √2 vs. π. Both are irrational numbers — their decimal expansions never end and never repeat. But √2 = 1.41421… can be computed by a simple iterative squaring algorithm. Does that mean π is “more infinite” than √2? (Hint: look up the difference between algebraic and transcendental irrationals.)
  3. The Halting Problem. Spock’s gambit relies on the computer never recognising that the task is impossible. A computer that could detect infinite loops would not be trapped. Alan Turing proved in 1936 that no such detector can exist. Why not?
  4. Faster algorithms. The Chudnovsky algorithm computes roughly 14 correct decimal places of π per term. The Leibniz series needs about 10,000 terms for 4 decimal places. Write the Chudnovsky series from its Wikipedia entry — how is its sigma notation similar to and different from the Leibniz series?
  5. The error bound. The error after n terms is at most 4 / (2n + 1). How many terms would you need for the approximation to be accurate to 6 decimal places? (Solve for n. Check your answer with the slider.)