Continued Fractions & Pell's Equation

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

The Best Approximations

A continued fraction finds the best fractions for any real number. It explains why 355/113 is so close to π, and it cracks an equation Archimedes posed.

Continued Fraction Builder

Pick a number and build its expansion term by term. The convergents land alternately above and below it, closing in each time.

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

Go deeper:Continued Fraction Builder

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.

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.

Convergence Rate

Compare how fast the error shrinks for π and for the golden ratio φ. Big terms mean big jumps; φ's are all 1, so it converges slowest.

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
Go deeper: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.

Pell's Equation Solver

Slide D to solve x² - Dy² = 1. Watch the periodic expansion of √D produce the smallest solution, and find it on the hyperbola.

√2 = [1; 2](period 1)
Fundamental solution
x = 3, y = 2
3² - 2 × 2² = 1
(3,2)(17,12)(99,70)(577,408)
#xyx² - 2y²
1321
217121
399701
45774081
5336323781
Go deeper: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 fundamental solution is a convergent: the one at the end of the first period if the period length is even, or at the end of the second period if it is odd (the first then solves x² - Dy² = -1 instead). All other solutions are generated by "powers" of it.

Ford Circles

Raise the maximum denominator and zoom into a range. Tangent circles mark neighbouring fractions, and smaller circles nestle in every gap.

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.

Go deeper: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.

Key Takeaways

  • 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

Spot a mistake or have an idea?

Search