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.

8 chapters9 live explorations6 Sage notebooks70+ exercises

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.

01

Euclid · Bézout · Diophantine equations

The algorithm in a rectangle

Turn repeated division into a proof-producing machine.
02

Unique factorization · √2 irrational · Eratosthenes

Every integer has an address

Use prime exponents for proof, then uncover primes with the sieve.
03

Lagrange–Galois · Pell · Archimedes

Fractions inside fractions

Prove why surds repeat, locate Pell solutions, and count the Cattle of the Sun.
04

Extended gcd · CRT · Interpolation

Arithmetic on a clock

Certify inverses, reconstruct remainders, and prove that Fibonacci residues repeat.
05

Fermat and Euler · Möbius inversion

A theorem you can rotate

Count rotations, recover exact periods, and connect inversion with interpolation.
06

Groups · Cosets · Lagrange · Euler

The reusable machine

Name the structure shared by clocks and rotations.
07

Binomial coefficients · Valuations · Kummer

Arithmetic in Pascal’s triangle

Find prime divisibility hiding in base-p carries.
08

Central coefficients · θ(x) · Chebyshev

How crowded are the primes?

Use the middle of Pascal’s triangle to count primes.
Designed for a first proof-oriented courseTwo 75-minute meetings per week · no calculus or abstract algebra required
Download the expanded course packet

Interactive companion

Touch the structure.

Every experiment is paired with a proof question. Computation is the microscope—not the oracle.

01

Algorithm lab

Tile the rectangle

Change the side lengths. The picture and proof trace update together.

391 × 299final tile: 23 × 23
gcd = 23
391=1 · 299+92
299=3 · 92+23
92=4 · 23+0
Bézout certificate23 = -3(391) + 4(299)

The same data, read forward: 391/299 = [1; 3, 4]. These partial quotients generate convergents whose signed coefficients reproduce the Euclidean remainders.

02

Approximation lab

Build a continued fraction

Watch simple fractions close in on an irrational target.

[1;2,2,2,2,2,2]
11below · error 4.1e-1
32above · error 8.6e-2
75below · error 1.4e-2
1712above · error 2.5e-3
4129below · error 4.2e-4
9970above · error 7.2e-5
239169below · error 1.2e-5

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.

03

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.

Alignment nowVenus has gained 0 whole laps“Conjunction” is simply the astronomical word for this alignment.
Earth’s laps0.00approaches 8
Venus’s laps0.00approaches 13
Venus’s gain0.00approaches 5

Why the alignment repeatsfaster lap rate − slower lap rate = overtaking rate

1/S = 1/224.7 − 1/365.256so S ≈ 583.92 days

Why a star appearsone repeat takes about 8/5 Earth laps, leaving a 3/5-turn step

13 − 8 = 5

Approximation, 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.

04

Congruence lab

Wind around a clock

Add on the clock, then use extended gcd to decide whether division is possible.

mod12
01234567891011
9 + 5≡2(mod 12)

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

15711

Multiplication by 5 rearranges these units:

1 → 5;5 → 1;7 → 11;11 → 7

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 →

05

Symmetry lab

Rotate a theorem

Color the beads, inspect the orbit, and see why prime length forces equal packets.

tap a bead

Rotational orbit · 5 distinct words

0
1
2
3
4
Nonconstant 3-color words35 − 3 = 240240 ÷ 5 = 48 complete orbits

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 →

06

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.

07

Sieve and prime-counting lab

Cross out composites, then count

Begin with Eratosthenes, then compare exact prime counts with the scale predicted by Chebyshev.

234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950

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.

π(x)168exact
x / log x144.8prediction
ratio1.161π(x) ÷ prediction
π(x)x / log x
Chebyshev’s deliberately broad proved interval50.2 ≤ π(1000) ≤ 579.1
08

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.

1a11a21a31a462a₀

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.

First positive solution of x² − 13y² = 1x = 649
y = 180

Odd period: one period gives −1; two periods give +1.

Two periods of convergents. Every entry is an exact integer.
kakpk / qkQk+1pk² − Dqk²
033 / 14-4
114 / 13+3
217 / 23-3
3111 / 34+4
4118 / 51-1 · Pell
56119 / 334+4
61137 / 383-3
71256 / 713+3
81393 / 1094-4
91649 / 1801+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.

09

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.

  1. ProportionsTotal cattle = 50,389,082t.
  2. SquareWhite + black bulls force t = 4,456,749r².
  3. 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.

X1 mod 9,3149063
Z1 mod 9,3147708

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).

Practice 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.

01

Fluency

Focused calculations make later proofs readable.

Example
Express gcd(527, 341) as 527x + 341y.
02

Reasoning

Intermediate claims guide students through complete proofs.

Example
Why does a nonconstant word of prime length have exactly p rotations?
03

Exploration

Generate evidence, form a conjecture, then test its limits.

Example
Color 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.

01 · Foundations and extensions

Euclid & continued fractions

The sieve, Bézout, convergents, matrix identities, Pell, Khinchin–Lévy experiments, and Gosper’s arithmetic.

Download notebook ↓
02 · Arithmetic and symmetry

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 ↓
03 · Prime divisibility

Pascal & prime counting

Explore carries, binomial coefficients, and the Chebyshev scale.

Download notebook ↓
04 · New focused lab

Möbius, Lagrange & Galois

Compose matrices, conjugate a period, detect repeated integer states, test reduced surds, and reverse a period.

Download notebook ↓Sage script ↓
05 · New focused lab

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 ↓
06 · Counts and inverses

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.

∴