Continued fractions

Continued fractions

Every real number is a₀ + 1/(a₁ + 1/(a₂ + …)) with whole numbers aₖ, written [a₀; a₁, a₂, …]. Rationals stop, quadratic irrationals repeat forever, and the cut-off fractions are the best rational approximations there are.

These functions moved here from @ratmath/algebraic, which still provides the algebraic numbers they work on.

Exact terms for any algebraic number

For an algebraic xx, the root of pp in an interval, the next term is t=⌊x⌋t = \lfloor x \rfloor: shrink the interval until it lies between two integers. Then x=t+1/yx = t + 1/y, and yy is a root of

q(y)=yd p(t+1/y), q(y) = y^d\, p(t + 1/y),

pp shifted by tt with its coefficients reversed, in the interval mapped by y=1/(x−t)y = 1/(x - t). No digit of xx is approximated, so every term is right. 23=[1;3,1,5,1,1,4,1,… ]\sqrt[3]{2} = [1; 3, 1, 5, 1, 1, 4, 1, \dots] shows no pattern, and nobody knows whether its terms are bounded.

import { continuedFraction, convergents, periodicContinuedFraction, fromContinuedFraction, pell, fundamentalUnit } from "@ratmath/numtheory";
import { nthRoot, sqrt, fromRational } from "@ratmath/algebraic";

continuedFraction(nthRoot(fromRational(2), 3), 8);   // [1n, 3n, 1n, 5n, 1n, 1n, 4n, 1n]
continuedFraction("103993/33102");                   // [3n, 7n, 15n, 1n, 292n]: a convergent of π
convergents([1, 2, 2, 2]).map(String);               // ["1", "3/2", "7/5", "17/12"]

The convergents pk/qkp_k/q_k satisfy pk=akpk−1+pk−2p_k = a_k p_{k-1} + p_{k-2}, qk=akqk−1+qk−2q_k = a_k q_{k-1} + q_{k-2}, and ∣x−pk/qk∣<1/qk2|x - p_k/q_k| < 1/q_k^2. Each is closer to xx than any fraction with a smaller denominator. A large next term means an unusually good convergent: π\pi's 355/113 comes right before the term 292.

Quadratic irrationals repeat

Lagrange: the expansion is eventually periodic exactly when xx is a quadratic irrational. Written as x=(P+D)/Qx = (P + \sqrt D)/Q with Q∣D−P2Q \mid D - P^2, each step is

a=⌊P+DQ⌋,P′=aQ−P,Q′=D−P′2Q, a = \left\lfloor \frac{P + \sqrt D}{Q} \right\rfloor, \qquad P' = aQ - P, \qquad Q' = \frac{D - P'^2}{Q},

all in integers. Only finitely many pairs (P,Q)(P, Q) occur, so one repeats, and the period is read off exactly.

periodicContinuedFraction(sqrt(fromRational(7)));   // { preperiod: [2n], period: [1n, 1n, 1n, 4n] }
fromContinuedFraction([1], [2]);                    // √2 exactly: [1; 2, 2, 2, …]

Pell's equation and units

x2−Dy2=1x^2 - Dy^2 = 1 (DD not a square) always has solutions, and the smallest comes from the period of D\sqrt D: if the period has length LL, the convergent just before the end of the first period gives p2−Dq2=(−1)Lp^2 - Dq^2 = (-1)^L. For odd LL that solves the −1-1 equation, and a second period solves the +1+1 equation. D=61D = 61 is Fermat's challenge to the English mathematicians. The same computation gives the fundamental unit ε>1\varepsilon > 1 of the integers of Q(d)\mathbb Q(\sqrt d): every unit is ±εn\pm\varepsilon^n (Dirichlet). The Pell page solves x2−Dy2=Nx^2 - Dy^2 = N for any NN.

pell(61);              // { x: 1766319049n, y: 226153980n, negative: { x: 29718n, y: 3805n } }
fundamentalUnit(94);   // { unit: 2143295 + 221064√94, norm: 1 }
Try itEdit the code and run it with @ratmath/numtheory and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.
Ctrl/⌘ + Enter runs