Continued fractions of rational numbers
- Cost
- for the expansion of a fraction with -bit numerator and denominator, and for its evaluation from the terms.
The continued fraction of a rational number is the sequence of quotients of the Euclidean algorithm. FLINT computes it in quasi-linear time by the half-GCD method [Sch1971]. For the basic theory see [HarWri2008, Chapter X] and [GKP1994, §6.7].
Continued fractions and matrices#
For with , the Euclidean algorithm , , with gives the expansion , with and for , and if (the shorter of the two expansions). Writing
the fractions are the convergents of , and for every
So the first terms of are a matrix of determinant with nonnegative entries (apart from the first row when ), and the rest of the expansion is that of , where acts as a Möbius transformation. The entries of have about as many bits in total as the terms consumed, and has correspondingly fewer bits than .
Terms from leading bits#
The key observation, due to Lehmer [Leh1938], is that the first terms of depend only on its leading bits.
Lemma 1. Let be an interval with , and let . Then every has first term , and is again an interval. More generally, the terms common to the expansions of and (as long as both remainders stay above 1) are terms of every .
Proof. For , , so , and is decreasing on . The general statement follows by induction on the number of terms.
If with of bits and are the leading bits of and (so , , and similarly for ), then lies in the interval , whose endpoints have bits. Expanding both endpoints in parallel until they disagree gives about bits worth of terms of at the cost of a computation on -bit numbers. Applying the resulting matrix to the full then reduces it to a pair of about bits.
The half-GCD algorithm#
Input: of bits.
Output: a matrix , a product of elementary matrices , and of about bits, such that the terms are terms of (and of every number in ).
- If is small, run Lehmer's algorithm with word-size matrices and return.
- Recursively apply the algorithm to the top bits of , giving with entries of about bits; set , now of about bits.
- Perform one division step, so that the next quotient is not split.
- Recursively apply the algorithm to the top bits of the new pair, giving ; set .
- Return and .
Each level costs a constant number of multiplications of -bit numbers, so the cost is , against for the Euclidean algorithm. The half-GCD algorithm for integer GCDs, which this one parallels, is described on Integer GCD. As with all half-GCD algorithms, the delicate part is the correction when the leading bits produce a term that is not a term of the full number; FLINT avoids it by working with intervals throughout (Lemma 1), so that every term written is guaranteed.
Evaluation#
Evaluating amounts to computing the matrix product . Multiplying the factors in order costs for a -bit result, since the partial products grow linearly. Splitting the product in halves recursively balances the sizes of the operands, and the cost becomes , as in Binary splitting and product trees.
The simplest fraction in an interval#
The Stern–Brocot tree contains every positive rational once: its root is , and the children of a node are obtained as mediants of the node and its nearest ancestors [GKP1994, §4.5]. A path in the tree is a continued fraction expansion, read as run lengths of left and right moves.
Proposition 2. Let , and let be the first node on the path of the Stern–Brocot tree from the root towards that lies in . Then every fraction satisfies and . In particular is the simplest fraction in .
Proof. The search starts with the bounds and and keeps two bounds with , replacing one of them by the mediant as long as the mediant is outside ; so stays inside the open interval . Since , every fraction strictly between and can be written with integers , so its numerator is at least and its denominator at least . When the mediant lies in , it attains both bounds.
In terms of continued fractions, the common terms of and are the common part of the two paths. FLINT computes them with the interval version of the half-GCD algorithm, which handles a ball directly (_fmpq_ball_get_cfrac), and then completes the expansion with the smallest integer in the transformed interval. Negative and mixed-sign intervals are reduced to this case by subtracting an integer or noting that or the smallest integer in the interval is the answer.
Farey neighbours#
Two fractions with are adjacent in every Farey sequence of order with . Given and , the left neighbour is therefore determined by and , which fmpq_farey_neighbors solves with one modular inverse; the right neighbour follows from the mediant relation with [HarWri2008, Chapter III].
Dedekind sums#
By the reciprocity law of Dedekind sums [RadGro1972], is a simple function of the continued fraction of : with the partial quotients and the convergent matrix of ,
[Hickerson1977]. FLINT evaluates it with word-size quotients when fits in a word and otherwise with the half-GCD code, which accumulates the alternating sum of the terms instead of storing them. The cost is that of a GCD.
Use in FLINT#
fmpq_get_cfraccomputes the terms with_fmpq_ball_get_cfrac, the interval half-GCD (with_fmpq_hgcdas its recursive kernel and Lehmer's method on one and two words below), then finishes with division steps;fmpq_get_cfrac_naiveis the Euclidean algorithm.fmpq_set_cfracmultiplies the elementary matrices in a balanced tree, with plain products below 32 terms.fmpq_simplest_between,fmpq_dedekind_sumand the large-modulus path of rational reconstruction (_fmpq_reconstruct_fmpz_2, see Rational reconstruction) use the same half-GCD code.fmpq_farey_neighborsuses the modular inverse formula above.
void fmpq_set_cfrac(fmpq_t x, const fmpz * c, slong n)void fmpq_simplest_between(fmpq_t x, const fmpq_t l, const fmpq_t r)void fmpq_farey_neighbors(fmpq_t l, fmpq_t r, const fmpq_t x, const fmpz_t Q)void fmpq_dedekind_sum(fmpq_t s, const fmpz_t h, const fmpz_t k)Referenced from#
- Integer GCD (algorithm)
- Rational reconstruction (algorithm)
- Continued fractions
fmpq_simplest_between,_fmpq_simplest_between- fmpq.h (module)
References#
- [Sch1971] A. Schönhage, "Schnelle Berechnung von Kettenbruchentwicklungen", Acta Informatica 1 (1971), 139–144
- [HarWri2008] G. H. Hardy and E. M. Wright, An Introduction to the Theory of Numbers, sixth edition, revised by D. R. Heath-Brown and J. H. Silverman, Oxford University Press, 2008.
- [GKP1994] R. L. Graham, D. E. Knuth and O. Patashnik, Concrete Mathematics, second edition, Addison-Wesley, 1994.
- [Leh1938] D. H. Lehmer, "Euclid's algorithm for large numbers", American Mathematical Monthly 45:4 (1938), 227–233.
- [RadGro1972] H. Rademacher and E. Grosswald, Dedekind Sums, Carus Mathematical Monographs 16, Mathematical Association of America, 1972.
- [Hickerson1977] D. Hickerson, "Continued fractions and density results for Dedekind sums", Journal für die reine und angewandte Mathematik 290 (1977), 113–116.
The FLINT team, “Continued fractions of rational numbers,” FLINT: Fast Library for Number Theory, documentation, version 3.7.0-dev, 2026. https://flintlib.org/doc/algorithms/number-theory/continued-fractions.html (generated 2026-11-02).
@misc{flint-doc-algorithms-number-theory-continued-fractions,
author = {{The FLINT team}},
title = {{Continued fractions of rational numbers}},
howpublished = {FLINT: Fast Library for Number Theory, documentation, version 3.7.0-dev},
year = {2026},
url = {https://flintlib.org/doc/algorithms/number-theory/continued-fractions.html},
note = {Generated 2026-11-02}
}