Continued Fractions

lean4-proofnumber-theoryvisualization
x=a0+1a1+1a2+x = a_0 + \cfrac{1}{a_1 + \cfrac{1}{a_2 + \cdots}}

Statement

A continued fraction (simple, regular) is an expression

x=a0+1a1+1a2+1a3+x = a_0 + \cfrac{1}{a_1 + \cfrac{1}{a_2 + \cfrac{1}{a_3 + \cdots}}}

written [a0;a1,a2,][a_0; a_1, a_2, \ldots], where each aia_i is a non-negative integer with ai1a_i \ge 1 for i1i \ge 1.

Theorem (rationality criterion). xx has a finite continued fraction [a0;a1,,an][a_0; a_1, \ldots, a_n] if and only if xQx \in \mathbb{Q}. 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 2=[1;2,2,2,]\sqrt{2} = [1; 2, 2, 2, \ldots] — convergents table:

The nn-th convergent pn/qnp_n/q_n is the rational approximation [1;2,2,,2][1; 2, 2, \ldots, 2] (nn twos after the semicolon):

| nn | ana_n | pnp_n | qnq_n | pn/qnp_n/q_n | Error pn/qn2|p_n/q_n - \sqrt{2}| | |-----|-------|-------|-------|------------|-------------------------------| | 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: pn=anpn1+pn2p_n = a_n p_{n-1} + p_{n-2}, qn=anqn1+qn2q_n = a_n q_{n-1} + q_{n-2}.

These are the best rational approximations to 2\sqrt{2}: no fraction p/qp/q with qqnq \le q_n is closer than pn/qnp_n/q_n.

Proof Sketch

  1. Apply the Euclidean algorithm to pp and qq (assuming x=p/qx = p/q in lowest terms). Each step p=a0q+rp = a_0 q + r gives one partial quotient a0=p/qa_0 = \lfloor p/q \rfloor.
  2. Replace xx by q/rq/r (the reciprocal of the remainder) and repeat. Since r<qr < q, the algorithm terminates in finitely many steps by Euclidean AlgorithmEuclidean Algorithmgcd(m,n)=gcd(nmodm,m)\gcd(m,n) = \gcd(n \bmod m, m)The oldest surviving algorithm, computing the greatest common divisor via iterated remaindersRead more →.
  3. The resulting sequence [a0;a1,,an][a_0; a_1, \ldots, a_n] converges to p/qp/q.
  4. Conversely, any finite [a0;,an][a_0; \ldots, a_n] is rational by induction.

Connections

The continued fraction algorithm is the Euclidean AlgorithmEuclidean Algorithmgcd(m,n)=gcd(nmodm,m)\gcd(m,n) = \gcd(n \bmod m, m)The oldest surviving algorithm, computing the greatest common divisor via iterated remaindersRead more → in disguise. The convergents pn/qnp_n/q_n satisfy pn/qnx<1/qn2|p_n/q_n - x| < 1/q_n^2, which is the content of the Cauchy CriterionCauchy Criterionε>0  N:  m,nN    aman<ε\forall\varepsilon>0\;\exists N:\; m,n \geq N \implies |a_m - a_n| < \varepsilonA 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