Thue's lemma


In modular arithmetic, Thue's lemma roughly states that every modular integer may be represented by a "modular fraction" such that the numerator and the denominator have absolute values not greater than the square root of the modulus.
More precisely for every pair of integers with, given two positive integers and such that, there are two integers and such that
and
Usually, one takes and equal to the smallest integer greater than the square root of, but the general form is sometimes useful, and make the unicity theorem easier to state.
The first known proof is attributed to, who used a pigeonhole argument. It can be used to prove Fermat's theorem on sums of two squares by taking m to be a prime p that is 1 mod 4 and taking a to satisfy a2 + 1 = 0 mod p.

Uniqueness

In general, the solution whose existence is asserted by Thue's lemma is not unique. For example, when there are usually several solutions, providing that and are not too small. Therefore, one may only hope unicity for the rational number, to which is congruent modulo if y and m are coprime. Nevertheless, this rational number needs not to be unique; for example, if, and, one has the two solutions
However, for and small enough, if a solution exists, it is unique. More precisely, with above notation, if
and
with
and
then
This result is the basis for rational reconstruction, which allows using modular arithmetic for computing rational numbers for which one knows bounds for numerators and denominators.
The proof is rather easy: by multiplying each congruence by the other and subtracting, one gets
The hypotheses imply that each term has an absolute value lower than, and thus that the absolute value of their difference is lower than. This implies that, and thus the result.

Computing solutions

The original proof of Thue's lemma is not efficient, in the sense that it does not provide any fast method for computing the solution.
The extended Euclidean algorithm, allows us to provide a proof that leads to an efficient algorithm that has the same computational complexity of the Euclidean algorithm.
More precisely, given the two integers and appearing in Thue's lemma, the extended Euclidean algorithm computes three sequences of integers, and such that
where the are non-negative and strictly decreasing. The desired solution is, up to the sign, the first pair such that.