The optimal way to approximate a real number by rationals
Every real number has a simple continued fraction expansion, finite exactly when the number is rational. Its convergents are the best rational approximations of their height — no fraction with a smaller denominator comes closer. Lagrange's theorem characterises the expansions that are eventually periodic as exactly those of quadratic irrationals, which is why continued fractions solve Pell's equation, compute fundamental units of real quadratic fields and drive an early factoring method.
Learning objectives
- Compute a continued fraction expansion and its convergents.
- State and use the best-approximation property.
- Apply Lagrange's theorem to recognise quadratic irrationals.
- Explain the role of the expansion of √D in Pell's equation.
- Manage precision correctly when expanding a floating-point number.
Section 01Expansion and convergents
The expansion of a real x is generated by repeatedly taking the integer part and inverting the remainder:
The convergents pn/qn follow a linear recurrence that requires no fraction arithmetic:
pnqn−1 − pn−1qn = (−1)n−1. Consecutive convergents are automatically in lowest terms and form a Bézout pair — which is the precise sense in which continued fractions and the extended Euclidean algorithm are the same computation.
Section 02Best approximation
The convergents are optimal approximations: if q ≤ qn and p/q ≠ pn/qn, then p/q is strictly further from x. The error satisfies
A large partial quotient an+1 means the preceding convergent is exceptionally good — the classical example is the approximation of π that arises from the partial quotient 292. This is the mechanism behind integer relation detection: an unexpectedly large partial quotient signals hidden rational structure.
Section 03Periodicity and quadratic irrationals
Lagrange's theorem: a continued fraction expansion is eventually periodic if and only if the number is a quadratic irrational. For √D with D not a perfect square, the expansion is purely periodic after the first term and has a palindromic body.
The expansion is computed exactly, without floating point, by tracking a triple (P, Q, a) representing (P + √D)/Q:
- Set a0 ← ⌊√D⌋, P ← a0, Q ← D − a02.
- If Q = 0 then D is a perfect square; stop.
- Set a ← ⌊(a0 + P)/Q⌋. All quantities remain integers throughout.
- Set P' ← aQ − P and Q' ← (D − P'2)/Q.
- Emit a; set (P, Q) ← (P', Q') and repeat from step 3.
- The period ends when (P, Q) returns to its first recurring value.
The fundamental solution of x2 − Dy2 = 1 appears as a convergent at the end of the first period. This is also the fundamental unit of the associated real quadratic order, which is why the regulator of a real quadratic field is a continued fraction computation.
ReferenceFrequently asked questions
Why avoid floating point when expanding an algebraic number?
Because each step multiplies the relative error by roughly the square of the denominator. After a few dozen terms a double-precision expansion is producing partial quotients that are pure noise. Quadratic irrationals should be expanded with the exact integer triple recurrence; other algebraic numbers need interval arithmetic with a certified precision budget.
How long is the period of the expansion of a square root?
It grows roughly like the square root of D, which makes the classical continued fraction method for regulators exponential in the size of the discriminant. This is exactly the limitation that Shanks's infrastructure method and the sub-exponential algorithms were designed to overcome.
Do continued fractions still matter given LLL?
Yes. LLL generalises the idea to higher dimension, but in one dimension continued fractions are faster, exact and optimal. They also remain the natural language for real quadratic fields, where the infrastructure is built directly on the expansion.
NavigateContinue in this stream
Curated next steps from this page. The site also surfaces algorithmically related reading below.
ProvenanceSources and further reading
This page is an original KEVOS explanatory article. It presents the underlying mathematics — definitions, algorithms, complexity results and selection criteria — in KEVOS editorial voice. No text is reproduced from any copyrighted source. Where numerical tables are relevant, KEVOS links to live authoritative databases rather than republishing static values.
