Suppose is prime. Fermat’s theorem tells us that we can write
. But how do we find the two integers? For a small prime we can try a few squares. For a large prime we would like something more systematic. There is a particularly nice algorithm that starts with a square root of minus one modulo
and uses nothing more than the Euclidean algorithm. There are really two problems here. The first is to find a square root of minus one modulo the prime. The second is to turn that modular root into a representation of the prime itself. We shall concentrate on the second problem first. At the end we will return to the first. This separation is useful because a modular root can be found by modular arithmetic, whereas finding two small integers seems at first to be a problem of a different kind.
Suppose , with
. Imagine that we have found
. If
, we are finished. Usually, however,
is quite large. Can we somehow replace the pair
by a smaller pair without losing the fact that the sum of its squares is divisible by the prime? It is worth keeping this question in mind as we work through the examples.
For instance, take . We can choose
, since
. We already have a sum of two squares divisible by
. What we would like to do is find two smaller integers whose squares add up to
itself. Let us apply the Euclidean algorithm to
and
. The complete calculation is
Normally, we carry out the Euclidean algorithm until the remainder becomes zero. The last nonzero remainder gives us the greatest common divisor. Here, however, we are interested in something different. Look at the sequence of successive remainders:
Since , the first remainder that falls below
is
. Let us stop at this point and ask whether
could be one of the two integers we are looking for. If it is, then the difference
must be a perfect square. Indeed,
We have obtained the desired representation:
Why did we stop at ? And why should the difference be a perfect square? At the moment, there is no obvious reason for this to work. Before trying to explain it, let us experiment with a few more primes. In each case, we shall start with a square root of
modulo the prime, perform the Euclidean algorithm, and examine what happens when the remainder first falls below
.
Let us do the divisions explicitly in two of these examples. For seventy-three, the first division leaves a remainder of nineteen, which is still too large. The second leaves eight, which is small enough:
The square root of seventy-three lies between eight and nine. Subtracting sixty-four from seventy-three leaves nine. For ninety-seven, only one division is needed:
For the larger example of 1009, we need three divisions:
Here the square root of 1009 is between thirty-one and thirty-two. The first two remainders are too large, but twenty-eight is small enough. The difference is the square of fifteen. The rule works even when the first modular root is larger than the prime divided by two. For instance, we could have chosen
instead of twelve for the prime twenty-nine. The divisions would be
We obtain the same five. Choosing the smaller of the two modular roots is convenient, but is not necessary. Why should this work? The interesting point is that a remainder carries more information than its size. In our first example, . Reducing modulo twenty-nine and squaring gives
. The second square comes from the coefficient of twelve. This suggests keeping track of that coefficient as we run the Euclidean algorithm.
Let us pause over the example of seventy-three. We have and
. Thus eight is congruent to three times our original modular root. We immediately get
The second integer in the representation is therefore not something that we need to find by taking a square root at the end. It is already present in the Euclidean calculation. For 1009, the corresponding calculations are
The last line gives . Both twenty-eight and fifteen are smaller than the square root of 1009. Their squared sum is therefore smaller than twice 1009. Since it is a positive multiple of 1009, it has to be 1009 itself. This suggests what we should try to prove in general: at the first small remainder, its accompanying coefficient is also small.
Why the algorithm works
To understand why this procedure works, let us look more carefully at what happens during the Euclidean algorithm. Recall that our starting point is an integer satisfying
. This means that whenever we find two integers
satisfying
we automatically have
So there is an easy way to construct sums of two squares divisible by . Choose an integer
, multiply it by
, and subtract some multiple of
to obtain
. The difficulty is to choose the integers in such a way that both
and
are small. Now return to the Euclidean algorithm. Take, for example,
and
. The first division gives
We usually think of
simply as a remainder. But let us write the equality in a slightly different way:
This tells us that . We have therefore found a pair of integers,
and
, whose squares add up to a multiple of
. Indeed,
This is already an improvement over our original sum . Can we make it smaller still? The next step of the Euclidean algorithm gives
Substituting the expression we obtained for
, we find
Thus , and we obtain another sum of two squares divisible by
:
We are getting closer. Let us perform one more division: This time we obtain
What is interesting about these calculations is that the Euclidean algorithm is producing two useful integers at each step, even though we usually pay attention to only one of them. The first is the remainder itself. The second is the coefficient of when we express that remainder in terms of our original two integers. Let us now describe this systematically. Write the successive remainders as
. Every subsequent remainder is obtained by subtracting a multiple of one remainder from the preceding one:
Since we begin with and
, and perform only integer subtractions, every remainder must be an integer linear combination of these two numbers. We can therefore write
We are particularly interested in the coefficient . The reason is that the other term,
, disappears when we reduce modulo
. Thus knowing
tells us that
How do we keep track of these coefficients without repeatedly substituting all the previous expressions? For the first two remainders, the coefficients are easy to determine. We have
So we begin with and
. Now suppose we already know the coefficients of two successive remainders. The next Euclidean division gives
Reading off the coefficient of , we obtain
This is convenient. We can calculate the coefficients by exactly the same recurrence that we use to calculate the remainders. There is no need to keep track of the coefficients if all we want is the congruence modulo
. Since
and
, we have at every step
In our example of , we can now write the two sequences side by side, together with the sums of squares they produce:
Notice what is happening. In the first column of remainders, the numbers become smaller and smaller. In the next column, the absolute values of the coefficients become larger and larger. The sum of their squares initially decreases, reaches , and then begins to increase again. This explains why it is not necessarily useful to run the Euclidean algorithm all the way to the remainder
. Although we have made one of the two integers very small, we have done so at the expense of making the other one large. What we really want is a stage at which both integers are small. In fact, we do not need a particularly sharp estimate. If we can arrange that both
and
are smaller than
, then
But we already know that this sum is divisible by . The only possibility is therefore
Our problem has now become much simpler. We no longer need to explain directly why subtracting the square of a remainder from produces another perfect square. We only need to understand the relationship between the decreasing remainders and the increasing coefficients. In particular, what happens to
at the moment when
first falls below
? It turns out that a simple identity in the Euclidean algorithm guarantees that the coefficient is still smaller than
at precisely this stage.
So at every step we have a sum of two squares divisible by the prime. We want both integers to be small. The remainders decrease, while the absolute values of the coefficients increase. Fortunately there is an exact relation between them. The recurrence gives
To see why the displayed identity holds, substitute the two recurrences on its left. The terms involving the quotient cancel:
The initial value of the determinant is . The signs of the coefficients alternate because the first two nonzero ones are
, and subtracting a positive multiple of a coefficient from one of the opposite sign preserves this pattern. The initial determinant is
, so its absolute value never changes. Moreover, the nonzero coefficients
alternate in sign. Hence, for positive remainders,
Now let be the first remainder smaller than
. The previous remainder is larger than
. The identity above gives
. Thus both integers are smaller than the square root of the prime. Their squared sum is a positive multiple of
smaller than
. There is only one such multiple. We conclude that
This explains the stopping rule. We stop at precisely the point where the remainder and its accompanying coefficient are both small enough. This is the simplest case of Cornacchia’s algorithm. Notice that the proof never used the best approximation theorem for continued fractions. Nor did it require us to know in advance that a representation exists. The determinant identity is enough. It provides an upper bound on the coefficient of the modular root in terms of the previous remainder, rather than the current one. This is exactly why we stop at the first crossing of the threshold, not at an arbitrary remainder below it.
Continued fractions
There is another way to discover the same algorithm. We want small integers satisfying
. Write
. Dividing by
gives
Thus we are looking for a good rational approximation to with a small denominator. Continued fractions do exactly this. For instance,
.
Its convergents begin with . The last of these gives
. Both fifteen and twenty-eight are smaller than
, giving
. The continued fraction is not searching for squares directly. It is finding a small denominator and a small approximation error. Our congruence turns these two quantities into a sum of squares.
For a smaller example, the fraction has continued fraction expansion
. Let us compute the approximation errors without using decimals. For a convergent
, the integer error is
.
Notice that the approximation improves as we go down the table, but the sum of the two squares does not necessarily decrease. Eventually, the denominator becomes too large. What we want is a convergent for which both the denominator and the integer error are small. Let us see this directly from the continued fraction expansion in our earlier example:
The successive convergents are
However, we do not need to compute all the convergents. For each fraction , we examine the integer error
. At the convergent
, we obtain
This is the first integer error whose absolute value is smaller than . The denominator is
. These are precisely the two integers we are looking for:
Thus we can obtain the representation directly from the continued fraction expansion of . We compute successive convergents
and stop as soon as
. The answer is then
This is really the same calculation as before, written in a different language. The Euclidean remainders are the absolute values of the integer errors, and the coefficients we kept track of are, up to sign, the denominators of the convergents. Continued fractions simply provide a natural way of organizing these two quantities together.
A geometric way to see the same argument
Consider the lattice . It is generated by
and
, so its fundamental parallelogram has area
. Every lattice point has squared length divisible by
. We only need to find a nonzero point with both coordinates smaller than
in absolute value. Why should we expect a short vector in this lattice? The area of a fundamental parallelogram is
. Thus, on average, there is one lattice point for every region of area
. A square of side roughly
has this area. This is only a heuristic at present: a fundamental parallelogram could be very thin and long. But we can turn the heuristic into an exact argument by looking at differences of points rather than trying to locate a lattice point directly.
There is a simple pigeonhole argument for this. Put and consider the
integer pairs
with both coordinates between zero and
. Assign to each pair the residue
. Two distinct pairs must receive the same residue. Their difference is a nonzero lattice point
with
. Its squared length is positive, divisible by
, and smaller than
. Hence it equals
. For example, when
and
, consider the thirty-six points with coordinates between zero and five. The points
and
both give the residue five, since
. Their difference is
. This is exactly the vector that our Euclidean calculation found.
The argument proves that some collision must occur, but searching for a collision among roughly pairs is not especially efficient. The Euclidean algorithm avoids that search. The Euclidean algorithm gives a much faster way of finding such a point. At each step we replace a lattice basis by another basis of the same lattice, subtracting an integer multiple of one vector from the other. In the example of twenty-nine, the successive vectors are
. The last two are perpendicular and have squared length twenty-nine. We started with long, nearly parallel vectors and ended with a square basis. There is a useful way to connect this geometric calculation with the determinant identity in the proof. At every stage the consecutive vectors
and
are a basis of the same lattice. Subtracting a multiple of one from the other is reversible by addition. Their determinant therefore remains equal to
up to sign. What appeared earlier as a cancellation of algebraic terms is simply preservation of area. When the second vector has first coordinate below the threshold, the first vector still has first coordinate above it. Since the second coordinates have opposite signs, the area is the sum of two nonnegative products. In particular, it bounds the product of the larger first coordinate and the new second coordinate. This forces the new second coordinate to be small.
Why a square? The relation implies that whenever
belongs to the lattice, so does
. The lattice is invariant under rotation through ninety degrees. Once we find a vector of squared length
, that vector and its rotation enclose a square of area
. This is the area of the original fundamental parallelogram, so the two vectors generate the entire lattice. In the language of Gaussian integers, this is precisely the assertion that the lattice is generated by the single Gaussian integer
. In fact, if
is a vector of squared length
and
, every lattice vector is uniquely of the form
with integers
. To justify the basis assertion directly, express the two vectors in the original basis. The resulting integer matrix has determinant one up to sign, because both parallelograms have the same area. Its inverse therefore has integer entries. Since the two vectors are perpendicular, we obtain
This explains not just the existence of the short vector but also the shape of the entire lattice. The vectors of squared length correspond precisely to the four choices
. The other root of minus one gives the reflected lattice. Every representation of the prime belongs to one of these two lattices, because neither coordinate can vanish modulo the prime and the ratio of the coordinates is a root of minus one. This proves uniqueness up to signs and interchange.
For completeness, the required modular root always exists: Wilson’s theorem gives . Pairing the factors on either side of
shows that
when
. Thus the argument also proves Fermat’s theorem. For large primes one can find the modular root by faster modular arithmetic; the Euclidean algorithm then extracts the representation in a logarithmic number of divisions. There is also a more practical way to find the modular root. Choose a nonzero residue
that is a quadratic nonresidue modulo
. Euler’s criterion gives
. Therefore we can take
. Half the nonzero residues are nonsquares, so random trials find one quickly on average, and repeated squaring computes the powers efficiently. For example, two is a nonresidue modulo twenty-nine, and
. This recovers the root used in our first example.
It is natural to try the same idea for . A square root of
modulo
again gives a lattice on which the quadratic expression is divisible by
. But now the region
is an ellipse rather than a circle, and the ninety-degree symmetry is lost when
. There need not be a point representing
itself. For example, minus five is a square modulo three, but three is not represented by
. This is where the distinction between different classes of quadratic forms begins to matter. The corresponding generalization of Cornacchia’s procedure is to stop at the first remainder
and test whether
is a nonnegative integer square. For example, for
, the root
satisfies
. The division
gives
, and
gives
. But for
, the root three gives the remainder one, and
is not even an integer. There is no representation: if the second coordinate is nonzero, it must have absolute value one, leaving two for the first square. The modular congruence alone is insufficient. This is the point at which the study of different classes of quadratic forms becomes necessary.