elementary number theory
Continued Fractions
You should know: rational numbers, euclidean algorithm
Overview
A continued fraction expresses a number as an integer plus the reciprocal of another number, applied recursively: a₀ + 1/(a₁ + 1/(a₂ + 1/(a₃ + ⋯))). Every rational number has a finite continued fraction expansion, and computing it is literally the Euclidean algorithm run on the numerator and denominator — the quotients at each division step are exactly the continued-fraction terms. Irrational numbers get infinite expansions; famously, the expansion of an irrational is eventually periodic if and only if the number is a quadratic irrational (root of a degree-2 polynomial with integer coefficients), a theorem due to Lagrange. Truncating the expansion at each stage produces the 'convergents,' which are provably the best possible rational approximations to a given number for their denominator size — the reason continued fractions are used for calendar design (fractions of a year), gear ratios, and solving Pell's equation x² − Ny² = 1.
Intuition
Computing the continued fraction of a fraction a/b is the Euclidean algorithm wearing a different hat: at each step you take the integer part (the quotient) and recurse on the reciprocal of the remainder, exactly mirroring 'divide, take the remainder, repeat.' Truncating the expansion early is like rounding a decimal, but far smarter — because each new term corrects the previous approximation by the largest amount possible for the least increase in denominator, convergents are 'best approximations' in a precise, provable sense (nothing with a smaller or equal denominator gets closer).
Formal Definition
For a real number x, define a₀ = ⌊x⌋ and recursively xₖ₊₁ = 1/(xₖ − aₖ), aₖ₊₁ = ⌊xₖ₊₁⌋, giving the expansion:
Worked Examples
Divide 47 by 17: quotient 2, remainder 13.
Recurse on 17/13: quotient 1, remainder 4.
Recurse on 13/4: quotient 3, remainder 1.
Recurse on 4/1: quotient 4, remainder 0 — expansion terminates.
Answer: 47/17 = [2; 1, 3, 4].
Practice Problems
Compute 17 mod 13.
Using [2; 1, 3, 4] for 47/17, compute the second convergent p₁/q₁ = [2; 1] and verify it approximates 47/17 ≈ 2.7647.
Find the continued fraction expansion of 7/3.
Quiz
Summary
- A continued fraction [a₀; a₁, a₂, …] expresses a number via nested reciprocals; for rationals, computing it is exactly the Euclidean algorithm.
- Truncations (convergents pₖ/qₖ) satisfy a linear recurrence and are the best possible rational approximations for their denominator size.
- Lagrange's theorem: an irrational's expansion is eventually periodic exactly when the number is a quadratic irrational — key to solving Pell's equation.
Mathematics