Continued Fractions & Pell's Equation

Explore the most natural representation of real numbers and solve x² - Dy² = 1

The Best Approximations

Every real number has a continued fraction expansion [a0; a1, a2, ...] that provides the best possible rational approximations. Unlike decimals, continued fractions reveal the true approximation complexity of a number.

For quadratic irrationals like √D, the expansion is always periodic — and this periodicity is the key to solving Pell's equation x² - Dy² = 1, one of the oldest problems in Diophantine equations. This topic connects to Galois Theory through units of real quadratic number fields.

Demo 1: Continued Fraction Builder

Watch a continued fraction expansion build term by term. Each convergent pk/qk is a "best rational approximation" — no fraction with a smaller denominator gets closer. Convergents alternate above and below the target, bracketing it ever more tightly.

[1; 2 (period 1)]
1
1
3
2
1.4142141/13/2

Convergents alternate above and below the target (red line), converging from both sides. Even convergents (amber) approach from below, odd (blue) from above.

Demo 2: Convergence Rate

How fast do convergents approach the target? The error |α - pk/qk| is always less than 1/qk². Numbers with large CF coefficients (like π) are well-approximated by simple fractions; those with small coefficients (like the golden ratio φ) are the hardest to approximate — making φ the "most irrational" number.

convergent index k|α - p_k/q_k| (log)error1/q²
kpk/qkvalueerror
03/13.000000001.42e-1
122/73.142857141.26e-3
2333/1063.141509438.32e-5
3355/1133.141592922.67e-7
4103993/331023.141592655.78e-10
5104348/332153.141592653.32e-10
6208341/663173.141592651.22e-10
7312689/995323.141592652.91e-11
8833719/2653813.141592658.72e-12
91146408/3649133.141592651.61e-12

Demo 3: Pell's Equation Solver

Pell's equation x² - Dy² = 1 has been studied since ancient times (Archimedes posed a version for D = 4729494). The continued fraction of √D is always periodic, and the convergent at the end of the period gives the fundamental solution. All other solutions are generated by "powers" of it.

2 = [1; 2](period 1)
Fundamental solution
x = 3, y = 2
3² - 2 × 2² = 1
(3,2)(17,12)(99,70)(577,408)
#xyx² - 2
1321
217121
399701
45774081
5336323781

Demo 4: Ford Circles

For each fraction p/q in lowest terms, draw a circle of radius 1/(2q²) tangent to the x-axis at p/q. The resulting Ford circles exhibit beautiful tangency relations: two circles are tangent if and only if their fractions are neighbors in some Farey sequence. Zoom in to see the fractal-like structure.

0/11/51/41/32/51/23/52/33/44/51/101/41/23/41

Ford circles are tangent to the x-axis at each fraction p/q (in lowest terms) with radius 1/(2q²). Two Ford circles are tangent to each other iff their fractions are Farey neighbors.

Journey Complete!

You've completed the Number Theory module! You now understand:

  • Continued fractions as best rational approximations
  • Convergence rates and the "most irrational" numbers
  • Pell's equation and its connection to periodic continued fractions
  • Ford circles and the geometric structure of rational numbers

From divisibility and primes through modular arithmetic, the zeta function, quadratic reciprocity, and continued fractions — you've explored the full landscape of classical number theory. Head to the Playground to experiment freely, or test yourself with the Quiz!