Euclid · Bézout · Diophantine equations
An interactive introduction to number theory
Patterns,
Proofs, and Primes
Start with a rectangle. Follow Venus around the Sun. Rotate a necklace. Then carry the patterns all the way to a proof.
compute
→notice
→conjecture
→prove
→reuse
The course
One idea unlocks the next.
The sequence is a proof journey, not a catalogue: algorithms become structures, structures become symmetry, and symmetry becomes information about primes.
Unique factorization · √2 irrational · Eratosthenes
Every integer has an address
Use prime exponents for proof, then uncover primes with the sieve.Lagrange–Galois · Pell · Archimedes
Fractions inside fractions
Prove why surds repeat, locate Pell solutions, and count the Cattle of the Sun.Extended gcd · CRT · Interpolation
Arithmetic on a clock
Certify inverses, reconstruct remainders, and prove that Fibonacci residues repeat.Fermat and Euler · Möbius inversion
A theorem you can rotate
Count rotations, recover exact periods, and connect inversion with interpolation.Groups · Cosets · Lagrange · Euler
The reusable machine
Name the structure shared by clocks and rotations.Binomial coefficients · Valuations · Kummer
Arithmetic in Pascal’s triangle
Find prime divisibility hiding in base-p carries.Central coefficients · θ(x) · Chebyshev
How crowded are the primes?
Use the middle of Pascal’s triangle to count primes.Interactive companion
Touch the structure.
Every experiment is paired with a proof question. Computation is the microscope—not the oracle.
Algorithm lab
Tile the rectangle
Change the side lengths. The picture and proof trace update together.
The same data, read forward: 391/299 = [1; 3, 4]. These partial quotients generate convergents whose signed coefficients reproduce the Euclidean remainders.
Approximation lab
Build a continued fraction
Watch simple fractions close in on an irrational target.
The alternating trap: the 1st, 3rd, 5th, … convergents rise below the target; the 2nd, 4th, 6th, … converge downward from above.
The matrix spine: multiplying the digit matrices [[a, 1], [1, 0]] places consecutive convergents in its columns. Every product has determinant ±1, so it can be reversed over the integers.
Linear fractional transformations: a matrix M = [[a, b], [c, d]] with ad − bc ≠ 0 acts by TM(t) = (at + b)/(ct + d). Composition is matrix multiplication, with the rightmost factor acting first. A repeating tail is a fixed point, giving ct² + (d − a)t − b = 0.
A prefix conjugates the period: if x = TN(x) and y = TM(x), then y = TMNM−1(y). The nontrivial period matrix stays nontrivial after conjugation, so an eventually periodic continued fraction is still a quadratic irrational. Chapter 3 works this out for √3.
Exactly which numbers repeat? Lagrange’s theorem says: precisely the real quadratic irrationals. Galois’s sharper criterion says the repetition starts immediately exactly when x > 1 and −1 < x′ < 0, where x′ is the other root of its quadratic. Thus √2 is eventually periodic, while 1 + √2 is purely periodic. The expanded book proves both directions using bounded integer states and unique reduced predecessors, with worked examples, exercises, and solutions. Sage Lab 4 turns these proofs into exact matrix and quadratic-field experiments.
A calendar is an approximation: the tropical year has about 0.2422 extra days. Its convergents begin 1/4, 7/29, 8/33, 31/128. The Julian rule is 1/4; after requiring a cycle of whole centuries, the convergent 97/4 to the per-century excess 100 × 0.2422 becomes the Gregorian rate 97/400.
Almost base 11: the Khinchin–Lévy law gives a typical precision factor of eπ²/(6 log 2) ≈ 10.731 per continued-fraction term—about 1.03 decimal digits.
π looks typical; e does not: for the first 100,000 partial quotients of π, the geometric mean is 2.67956 and qn2/n is 10.67835, close to the generic targets 2.68545 and 10.73102—but no limiting theorem is known for π. Euler’s patterned expansion of e instead makes both statistics diverge.
Continued fractions can calculate: Gosper’s homographic and bihomographic algorithms absorb input terms and emit certified output terms using only integer matrices. A modern interval-valued refinement prevents exact boundary cases—such as √2 × √2 = 2—from stalling forever.
Periodicity meets Pell: The identity pk² − Dqk² = (−1)k+1Qk+1 proves that a period endpoint gives norm ±1. The exact Pell lab shows the parity rule and the palindromic digits; Archimedes’ cattle add a divisibility condition to the resulting Pell equation.
Astronomy and approximation lab
Watch Venus gain five laps
Follow two planets for eight Earth years. Each time Venus gains one complete lap, a new conjunction draws the next edge of the star.
Why the alignment repeatsfaster lap rate − slower lap rate = overtaking rate
1/S = 1/224.7 − 1/365.256so S ≈ 583.92 daysWhy a star appearsone repeat takes about 8/5 Earth laps, leaving a 3/5-turn step
13 − 8 = 5Approximation, not clockwork: the animation uses modern mean periods. The fifth return misses the starting ray slightly because 13 Venus laps are only approximately 8 Earth laps. The Dresden Codex uses a different, exact calendar model: 5 × 584 = 8 × 365 = 2,920.
Congruence lab
Wind around a clock
Add on the clock, then use extended gcd to decide whether division is possible.
Extended gcd · a certificate for division
Try dividing by the step 5. Extended Euclid gives the integer identity 5 × (5) + 12 × (-2) = 1.
The coefficient 5 reduces to 5, the inverse of 5 modulo 12. Check: 5 × 5 ≡ 1 (mod 12).
Units modulo 12
Multiplication by 5 rearranges these units:
CRT becomes interpolation: a selector is 1 at one remainder and 0 at the others. Replace integer moduli with X − c and the same construction gives Lagrange’s interpolation formula. Extended gcd supplies the denominator inverses over a prime field. Read the construction and worked examples → At roots of unity, these Lagrange selectors give exactly the inverse discrete Fourier transform. Connect interpolation, Fourier inversion, and Möbius inversion →
Why Fibonacci residues repeat: follow the pair (a, b) → (b, a + b). Its inverse (u, v) → (v − u, u) makes every step reversible, so finitely many pairs force a cycle from the start. Return to (0, 1), not just a zero. Prove periodicity now → Explore extension fields and exact periods later →
Symmetry lab
Rotate a theorem
Color the beads, inspect the orbit, and see why prime length forces equal packets.
Rotational orbit · 5 distinct words
From Fermat to Euler: with two colors and nine positions, remove the 23 words that repeat a three-bead block. The remaining 29 − 23 = 504 words form 56 orbits of size 9. Cancel the invertible factor 23 to get 26 ≡ 1 (mod 9). The same count works at every prime power; CRT combines the congruences into Euler’s theorem. Follow the proof, starting with length 9 →
Undo a count: the totient is a multiplicative function, and its sum over the divisors of n is n. Möbius inversion recovers the individual contributions. The same formula counts words of each exact period. Explore multiplicative functions and divisor sums → Bonus: posets, inclusion–exclusion, and partition counts →
Fermat already gives the sum formula: in the prime field, (a + b)p = a + b = ap + bp. The power map is the identity there. The binomial proof makes this a polynomial identity valid in extensions, where Frobenius can move the new elements. See Frobenius act in a quadratic extension →
Divisibility lab
Color Pascal’s triangle
Reduce every entry modulo a prime. Base-p carries become a fractal you can see.
Look for it: blank cells are the coefficients divisible by 2. The repeating blocks are base-2 arithmetic made visible.
Sieve and prime-counting lab
Cross out composites, then count
Begin with Eratosthenes, then compare exact prime counts with the scale predicted by Chebyshev.
Sieve through 50: boxed numbers survive. Crossing out multiples of 2, 3, 5, and 7 is enough because every composite has a prime factor no larger than its square root.
Existence versus a witness: the classical argument with x = (√2)√2 proves by cases that some pair of irrationals has a rational power, but does not say which pair. The explicit choice a = √2 and b = log√23 gives ab = 3; unique factorization proves that b is irrational.
Exact quadratic lab
Which convergent solves Pell?
Read the palindrome, follow the complete quotients, and locate the first norm-one solution.
√13 has initial digit a0 = 3 and least period L = 5.
The palindromic part: remove the final 2a0; the remaining digits read the same from either end. Matching colors mark reflected positions. Galois’s criterion and reversal of the period of √D + a0 explain the symmetry.
y = 180
Odd period: one period gives −1; two periods give +1.
| k | ak | pk / qk | Qk+1 | pk² − Dqk² |
|---|---|---|---|---|
| 0 | 3 | 3 / 1 | 4 | -4 |
| 1 | 1 | 4 / 1 | 3 | +3 |
| 2 | 1 | 7 / 2 | 3 | -3 |
| 3 | 1 | 11 / 3 | 4 | +4 |
| 4 | 1 | 18 / 5 | 1 | -1 · Pell |
| 5 | 6 | 119 / 33 | 4 | +4 |
| 6 | 1 | 137 / 38 | 3 | -3 |
| 7 | 1 | 256 / 71 | 3 | +3 |
| 8 | 1 | 393 / 109 | 4 | -4 |
| 9 | 1 | 649 / 180 | 1 | +1 · Pell |
Why the right convergent must work
Write the next complete quotient as xk+1 = (Pk+1 + √D)/Qk+1. Substitute it into √D = (pkxk+1 + pk−1)/(qkxk+1 + qk−1). Comparing the rational and irrational parts, then using the determinant of the convergent matrix, gives the exact identity
pk² − Dqk² = (−1)k+1Qk+1.
The complete quotient has Qk+1 = 1 exactly when L divides k + 1. Therefore the Pell convergents are precisely those with k = mL − 1, and their sign is (−1)mL. For positive Pell, stop at k = L − 1 if L is even, or k = 2L − 1 if L is odd: just before the relevant final digit 2a0.
Read the complete proof and the theorem generating all solutions →
Try D = 61. A modest radicand can have a very large first solution. Explain its index using the period, then reproduce it in Sage Lab 5.
Archimedes’ Cattle of the Sun
A small remainder selects an enormous herd.
Seven proportions leave one scale factor. A square and a triangle turn that scale into a Pell problem.
- ProportionsTotal cattle = 50,389,082t.
- SquareWhite + black bulls force t = 4,456,749r².
- TriangleX² − 4,729,494Z² = 1, with Z = 9,314r.
The least positive unit ε = u + v√4,729,494 generates every positive Pell solution: εj = Xj + Zj√4,729,494. The cattle condition is 9,314 divides Zj. Follow the coefficients modulo 9,314 to find the first admissible exponent.
Not divisible: this Pell solution fails the cattle condition.
Minimality needs both steps. The residue recurrence checks every exponent from 1 through 2,329. Its first zero second coefficient occurs at j = 2,329. Since all positive Pell solutions are powers of ε and their second coefficients increase, this gives the smallest herd: 206,545 decimal digits, approximately 7.760271406 × 10206544.
Inspect the exact unit and the modular recurrence
u = 109931986732829734979866232821433543901088049
v = 50549485234315033074477819735540408986340
The continued fraction of √4,729,494 has period 92. Its convergent p91/q91 supplies (u, v). Modulo 9,314, the unit has coefficients (9,063, 7,708), and D is 7,296.
(a, b) ↦ (9,063a + 7,296 · 7,708b, 7,708a + 9,063b) mod 9,314.
At j = 2,329, the pair is (9,313, 0). The Sage notebook constructs the complete integer herd and verifies every original proportion, the square, and the triangle.
Follow the full reduction in the book → · Ilan Vardi, Archimedes’ Cattle Problem (preprint).
Ready to work through a problem?
Browse student problem sets & quizzesPractice architecture
From calculation to explanation.
Weekly work mixes three modes. The aim is not merely to get an answer, but to know what kind of reasoning produced it.
Fluency
Focused calculations make later proofs readable.
ExampleExpress gcd(527, 341) as 527x + 341y.
Reasoning
Intermediate claims guide students through complete proofs.
ExampleWhy does a nonconstant word of prime length have exactly p rotations?
Exploration
Generate evidence, form a conjecture, then test its limits.
ExampleColor Pascal’s triangle modulo p. What do its repeating blocks know about base p?
SageMath companion
Compute exactly. Then explain why.
Six notebooks pair executable calculations with proof questions. Open a notebook in Jupyter with the SageMath kernel and run from the first cell.
Euclid & continued fractions
The sieve, Bézout, convergents, matrix identities, Pell, Khinchin–Lévy experiments, and Gosper’s arithmetic.
Download notebook ↓Clocks, necklaces & groups
Extended gcd, CRT, interpolation, Fibonacci pairs, and Euler’s theorem through necklaces. Later: extensions, Frobenius, and exact periods. Runs in SageMath or Python.
Download notebook ↓Download script ↓Pascal & prime counting
Explore carries, binomial coefficients, and the Chebyshev scale.
Download notebook ↓Möbius, Lagrange & Galois
Compose matrices, conjugate a period, detect repeated integer states, test reduced surds, and reverse a period.
Download notebook ↓Sage script ↓Pell & the Cattle of the Sun
Prove the exact norm identity, inspect palindromes, generate Pell units, and verify Archimedes’ complete herd.
Download notebook ↓Sage script ↓Möbius & Fourier inversion
Recover totients and exact periods. Explore posets, partition coefficients, and Fourier inversion as interpolation at roots of unity. Runs in SageMath or Python.
Download notebook ↓Download script ↓With SageMath installed, start sage -n jupyterlab, open the downloaded notebook, and select the SageMath kernel. The downloadable scripts run with sage filename.sage; Labs 2 and 6 also run in standard Python. The notebooks distinguish computational evidence from the proof still required.
The central lesson
Proof is a way of seeing why a pattern had to appear.
Print the formal development. Use the web to experiment. Move between them until the computation and the proof tell the same story.