Continued Fractions
Statement
A continued fraction (simple, regular) is an expression
written , where each is a non-negative integer with for .
Theorem (rationality criterion). has a finite continued fraction if and only if . The algorithm is exactly the Euclidean algorithm applied to the numerator and denominator.
In Mathlib: GenContFract.terminates_of_rat states that (GenContFract.of q).Terminates for any q : ℚ.
Visualization
Continued fraction of — convergents table:
The -th convergent is the rational approximation ( twos after the semicolon):
| | | | | | Error | |-----|-------|-------|-------|------------|-------------------------------| | 0 | 1 | 1 | 1 | 1 | 0.414... | | 1 | 2 | 3 | 2 | 1.5 | 0.086... | | 2 | 2 | 7 | 5 | 1.4 | 0.014... | | 3 | 2 | 17 | 12 | 1.4167... | 0.0024... | | 4 | 2 | 41 | 29 | 1.4138... | 0.00042... |
Recurrence: , .
These are the best rational approximations to : no fraction with is closer than .
Proof Sketch
- Apply the Euclidean algorithm to and (assuming in lowest terms). Each step gives one partial quotient .
- Replace by (the reciprocal of the remainder) and repeat. Since , the algorithm terminates in finitely many steps by Euclidean AlgorithmEuclidean AlgorithmThe oldest surviving algorithm, computing the greatest common divisor via iterated remaindersRead more →.
- The resulting sequence converges to .
- Conversely, any finite is rational by induction.
Connections
The continued fraction algorithm is the Euclidean AlgorithmEuclidean AlgorithmThe oldest surviving algorithm, computing the greatest common divisor via iterated remaindersRead more → in disguise. The convergents satisfy , which is the content of the Cauchy CriterionCauchy CriterionA sequence converges if and only if its terms eventually become arbitrarily close to each other — no candidate limit requiredRead more → for Diophantine approximation.
Lean4 Proof
/-- The continued fraction of any rational number terminates.
Direct alias of `GenContFract.terminates_of_rat`. -/
theorem cf_rat_terminates (q : ℚ) : (GenContFract.of q).Terminates :=
GenContFract.terminates_of_rat q