# Lagrange's four-square theorem

{{#invoke:Hatnote|hatnote}} Template:Redirect2

Lagrange's four-square theorem, also known as Bachet's conjecture, states that any natural number can be represented as the sum of four integer squares.

${\displaystyle p=a_{0}^{2}+a_{1}^{2}+a_{2}^{2}+a_{3}^{2}\ }$

where the four numbers ${\displaystyle a_{0},a_{1},a_{2},a_{3}}$ are integers. For illustration, 3, 31 and 310 can be represented as the sum of four squares as follows:

${\displaystyle 3=1^{2}+1^{2}+1^{2}+0^{2}}$
${\displaystyle 31=5^{2}+2^{2}+1^{2}+1^{2}}$
${\displaystyle 310=17^{2}+4^{2}+2^{2}+1^{2}.}$

This theorem was proven by Joseph Louis Lagrange in 1770.

## Historical development

From examples given in the Arithmetica it is clear that Diophantus was aware of the theorem. This book was translated in 1621 into Latin by Bachet, who stated the theorem in the notes of his translation. But the theorem was not proven until 1770 by Lagrange.[1]

Adrien-Marie Legendre completed the theorem in 1797-8 with his three-square theorem, by proving that a positive integer can be expressed as the sum of three squares if and only if it is not of the form ${\displaystyle 4^{k}(8m+7)}$ for integers ${\displaystyle k}$ and ${\displaystyle m}$. Later, in 1834, Carl Gustav Jakob Jacobi discovered a simple formula for the number of representations of an integer as the sum of four squares with his own four-square theorem.

The formula is also linked to Descartes' theorem of four "kissing circles", which involves the sum of the squares of the curvatures of four circles. This is also linked to Apollonian gaskets, which were more recently related to the Ramanujan–Petersson conjecture.[2]

## The classical proof

Several very similar modern versions[3][4][5] of Lagrange's proof are easy to find in the literature. The proof below is a slightly simplified version, in which the cases for which m is even or odd do not require separate arguments.

It is sufficient to prove the theorem for every odd prime number p. This immediately follows from Euler's four-square identity (and from the fact that the theorem is true for the numbers 1 and 2).

The residues of a2 modulo p are distinct for every a between 0 and (p-1)/2 (included). To see this, take some a and define c as a2 mod p. a is a root of the polynomial x2 - c over the field 'Z/pZ'. So is p - a (which is different from a). In a field K, any polynomial of degree n has at most n distinct roots, so there are no other a with this property, in particular not among 0 to (p-1)/2.

Contingently, for b taking integral values between 0 and (p-1)/2 (included), the -b2-1 are distinct. By the pigeonhole principle, there are a and b in this range, for which a2 and -b2-1 are congruent modulo p, that is for which

a2 + b2 + 12 + 02 = n p ,

with 0<n< p.

Now let m be the smallest positive integer such that m p is the sum of four squares, x12 + x22 + x32 + x42 (we have just shown that there is some m (namely n) with this property, so there is a least one). We show by contradiction that m equals 1: supposing it is not the case, we prove the existence of a positive integer r less than m, for which r p is also the sum of four squares (this is in the spirit of the infinite descent[6] method of Fermat).

For this purpose, we consider for each xi the yi which is in the same residue class modulo m and between (–m + 1)/2 and m/2 (included). It follows that y12 + y22 + y32 + y42 = m r, for some positive integer r less than m.

Finally, another appeal to Euler's four-square identity shows that m p m r = z12 + z22 + z32 + z42, where each zi is divisible by m (indeed, since each xi is congruent to his corresponding yi, z1 is congruent modulo m to y12 + y22 + y32 + y42 = m r; for the same reason the other zi are also divisible by m). It follows that, for wi = zi/m, w12 + w22 + w32 + w42 = r p, and this is in contradiction with the minimality of m.

## Proof using the Hurwitz integers

One of the ways to prove the theorem relies on Hurwitz quaternions, which are the analog of integers for quaternions.[7] The Hurwitz quaternions consist of all quaternions with integer components and all quaternions with half-integer components. These two sets can be combined into a single formula

${\displaystyle \alpha ={\frac {1}{2}}E_{0}(1+\mathbf {i} +\mathbf {j} +\mathbf {k} )+E_{1}\mathbf {i} +E_{2}\mathbf {j} +E_{3}\mathbf {k} =a_{0}+a_{1}\mathbf {i} +a_{2}\mathbf {j} +a_{3}\mathbf {k} }$

where ${\displaystyle E_{0},E_{1},E_{2},E_{3}}$ are integers. Thus, the quaternion components ${\displaystyle a_{0},a_{1},a_{2},a_{3}}$ are either all integers or all half-integers, depending on whether ${\displaystyle E_{0}}$ is even or odd, respectively. The set of Hurwitz quaternions forms a ring; that is to say, the sum or product of any two Hurwitz quaternions is likewise a Hurwitz quaternion.

The (arithmetic, or field) norm ${\displaystyle \mathrm {N} (\alpha )}$ of a rational quaternion ${\displaystyle \alpha }$ is the nonnegative rational number

${\displaystyle \mathrm {N} (\alpha )=\alpha {\bar {\alpha }}=a_{0}^{2}+a_{1}^{2}+a_{2}^{2}+a_{3}^{2}}$

where ${\displaystyle {\bar {\alpha }}=a_{0}-a_{1}\mathbf {i} -a_{2}\mathbf {j} -a_{3}\mathbf {k} }$ is the conjugate of ${\displaystyle \alpha }$. Note that the norm of a Hurwitz quaternion is always an integer. (If the coefficients are half-integers, then their squares are of the form ${\displaystyle {\tfrac {1}{4}}+n:n\in \mathbb {Z} }$, and the sum of four such numbers is an integer.)

Since quaternion multiplication is associative, and real numbers commute with other quaternions, the norm of a product of quaternions equals the product of the norms:

${\displaystyle \mathrm {N} (\alpha \beta )=\alpha \beta ({\overline {\alpha \beta }})=\alpha \beta {\bar {\beta }}{\bar {\alpha }}=\alpha \mathrm {N} (\beta ){\bar {\alpha }}=\alpha {\bar {\alpha }}\mathrm {N} (\beta )=\mathrm {N} (\alpha )\mathrm {N} (\beta ).}$

For any ${\displaystyle \alpha \neq 0}$, ${\displaystyle \alpha ^{-1}={\bar {\alpha }}\mathrm {N} (\alpha )^{-1}}$. It follows easily that ${\displaystyle \alpha }$ is a unit in the ring of Hurwitz quaternions if and only if ${\displaystyle \mathrm {N} (\alpha )=1}$.

The proof of the main theorem begins by reduction to the case of prime numbers. Euler's four-square identity implies that if Langrange's four-square theorem holds for two numbers, it holds for the product of the two numbers. Since any natural number can be factored into powers of primes, it suffices to prove the theorem for prime numbers. It is true for ${\displaystyle 2=1^{2}+1^{2}+0^{2}+0^{2}}$. To show this for an odd prime integer ${\displaystyle p}$, represent it as a quaternion ${\displaystyle (p,0,0,0)}$ and assume for now (as we shall show later) that it is not a Hurwitz irreducible; that is, it can be factored into two non-unit Hurwitz quaternions

${\displaystyle p=\alpha \beta .}$

The norms of ${\displaystyle p,\alpha ,\beta }$ are integers such that

${\displaystyle \mathrm {N} (p)=p^{2}=\mathrm {N} (\alpha \beta )=\mathrm {N} (\alpha )\mathrm {N} (\beta )}$

and ${\displaystyle \mathrm {N} (\alpha ),\mathrm {N} (\beta )>1}$. This shows that both ${\displaystyle \mathrm {N} (\alpha )}$ and ${\displaystyle \mathrm {N} (\beta )}$ are equal to ${\displaystyle p}$ (since they are integers), and ${\displaystyle p}$ is the sum of four squares

${\displaystyle p=\mathrm {N} (\alpha )=a_{0}^{2}+a_{1}^{2}+a_{2}^{2}+a_{3}^{2}.}$

If it happens that the ${\displaystyle \alpha }$ chosen has half-integer coefficients, it can be replaced by another Hurwitz quaternion. Choose ${\displaystyle \omega =(\pm 1\pm \mathbf {i} \pm \mathbf {j} \pm \mathbf {k} )/2}$ in such a way that ${\displaystyle \gamma \equiv \omega +\alpha }$ has even integer coefficients. Then

${\displaystyle p=({\bar {\gamma }}-{\bar {\omega }})\omega {\bar {\omega }}(\gamma -\omega )=({\bar {\gamma }}\omega -1)({\bar {\omega }}\gamma -1).}$

Since ${\displaystyle \gamma }$ has even integer coefficients, ${\displaystyle ({\bar {\omega }}\gamma -1)}$ will have integer coefficients and can be used instead of the original ${\displaystyle \alpha }$ to give a representation of ${\displaystyle p}$ as the sum of four squares.

As for showing that ${\displaystyle p}$ is not a Hurwitz irreducible, Lagrange proved that any odd prime ${\displaystyle p}$ divides at least one number of the form ${\displaystyle u=1+l^{2}+m^{2}}$, where ${\displaystyle l}$ and ${\displaystyle m}$ are integers.[7] This can be seen as follows: since ${\displaystyle p}$ is prime, ${\displaystyle a^{2}\equiv b^{2}{\pmod {p}}}$ can hold for integers ${\displaystyle a,b}$, only when ${\displaystyle a\equiv \pm b{\pmod {p}}}$. Thus, the set ${\displaystyle X=\{0^{2},1^{2},\dots ,((p-1)/2)^{2}\}}$ of squares contains ${\displaystyle (p+1)/2}$ distinct residues modulo ${\displaystyle p}$. Likewise, ${\displaystyle Y=\{-(1+x):x\in X\}}$ contains ${\displaystyle (p+1)/2}$ residues. Since there are only ${\displaystyle p}$ residues in total, and ${\displaystyle |X|+|Y|=p+1>p}$, the sets ${\displaystyle X}$ and ${\displaystyle Y}$ must intersect.

The number ${\displaystyle u}$ can be factored in Hurwitz quaternions:

${\displaystyle 1+l^{2}+m^{2}=(1+l\;\mathbf {i} +m\;\mathbf {j} )(1-l\;\mathbf {i} -m\;\mathbf {j} ).}$

The norm on Hurwitz quaternions satisfies a form of the Euclidean property: for any quaternion ${\displaystyle \alpha =a_{0}+a_{1}\mathbf {i} +a_{2}\mathbf {j} +a_{3}\mathbf {k} }$ with rational coefficients we can choose a Hurwitz quaternion ${\displaystyle \beta =b_{0}+b_{1}\mathbf {i} +b_{2}\mathbf {j} +b_{3}\mathbf {k} }$ so that ${\displaystyle \mathrm {N} (\alpha -\beta )<1}$ by first choosing ${\displaystyle b_{0}}$ so that ${\displaystyle |a_{0}-b_{0}|\leq 1/4}$ and then ${\displaystyle b_{1},b_{2},b_{3}}$ so that ${\displaystyle |a_{i}-b_{i}|\leq 1/2}$ for ${\displaystyle i=1,2,3}$. Then we obtain

${\displaystyle \mathrm {N} (\alpha -\beta )=(a_{0}-b_{0})^{2}+(a_{1}-b_{1})^{2}+(a_{2}-b_{2})^{2}+(a_{3}-b_{3})^{2}\leq \left({\frac {1}{4}}\right)^{2}+\left({\frac {1}{2}}\right)^{2}+\left({\frac {1}{2}}\right)^{2}+\left({\frac {1}{2}}\right)^{2}={\frac {13}{16}}<1.}$

It follows that for any Hurwitz quaternions ${\displaystyle \alpha ,\beta }$ with ${\displaystyle \alpha \neq 0}$, there exists a Hurwitz quaternion ${\displaystyle \gamma }$ such that

${\displaystyle \mathrm {N} (\beta -\alpha \gamma )<\mathrm {N} (\alpha ).}$

The ring ${\displaystyle H}$ of Hurwitz quaternions is not commutative, hence it is not an actual Euclidean domain, and it does not have unique factorization in the usual sense. Nevertheless, the property above implies that every right ideal is principal. Thus, there is a Hurwitz quaternion ${\displaystyle \alpha }$ such that

${\displaystyle \alpha H=pH+(1-l\;\mathbf {i} -m\;\mathbf {j} )H.}$

In particular, ${\displaystyle p=\alpha \beta }$ for some Hurwitz quaternion ${\displaystyle \beta }$. If ${\displaystyle \beta }$ were a unit, ${\displaystyle 1-l\;\mathbf {i} -m\;\mathbf {j} }$ would be a multiple of ${\displaystyle p}$, however this is impossible as ${\displaystyle 1/p-l/p\;\mathbf {i} -m/p\;\mathbf {j} }$ is not a Hurwitz quaternion for ${\displaystyle p>2}$. Similarly, if ${\displaystyle \alpha }$ were a unit, we would have

${\displaystyle (1+l\;\mathbf {i} +m\;\mathbf {j} )H=(1+l\;\mathbf {i} +m\;\mathbf {j} )pH+(1+l\;\mathbf {i} +m\;\mathbf {j} )(1-l\;\mathbf {i} -m\;\mathbf {j} )H\subseteq pH}$

so ${\displaystyle p}$ divides ${\displaystyle 1+l\;\mathbf {i} +m\;\mathbf {j} }$, which again contradicts the fact that ${\displaystyle 1/p-l/p\;\mathbf {i} -m/p\;\mathbf {j} }$ is not a Hurwitz quaternion. Thus, ${\displaystyle p}$ is not Hurwitz irreducible, as claimed.

## Generalizations

Lagrange's four-square theorem is a special case of the Fermat polygonal number theorem and Waring's problem. Another possible generalisation is the following problem: Given natural numbers ${\displaystyle a,b,c,d}$, can we solve

${\displaystyle n=ax_{1}^{2}+bx_{2}^{2}+cx_{3}^{2}+dx_{4}^{2}}$

for all positive integers ${\displaystyle n}$ in integers ${\displaystyle x_{1},x_{2},x_{3},x_{4}}$? The case ${\displaystyle a=b=c=d=1}$ is answered in the positive by Lagrange's four-square theorem. The general solution was given by Ramanujan. He proved that if we assume, without loss of generality, that ${\displaystyle a\leq b\leq c\leq d}$ then there are exactly 54 possible choices for ${\displaystyle a,b,c,d}$ such that the problem is solvable in integers ${\displaystyle x_{1},x_{2},x_{3},x_{4}}$ for all ${\displaystyle n}$. (Ramanujan listed a 55th possibility ${\displaystyle a=1,b=2,c=5,d=5}$, but in this case the problem is not solvable if ${\displaystyle n=15}$.[8])

## Algorithms

Michael O. Rabin and Jeffrey Shallit[9] have found randomized polynomial-time algorithms for computing a single representation ${\displaystyle n=x_{1}^{2}+x_{2}^{2}+x_{3}^{2}+x_{4}^{2}}$ for a given integer ${\displaystyle n}$, in expected running time ${\displaystyle \mathrm {O} ((\log n)^{2})}$.

## Number of representations

The number of representations of a natural number n as the sum of four squares is denoted by r4(n). Jacobi's four-square theorem states that this is eight times the sum of the divisors of n if n is odd and 24 times the sum of the odd divisors of n if n is even (see divisor function), i.e.

${\displaystyle r_{4}(n)={\begin{cases}8\sum \limits _{m|n}m&{\text{if }}n{\text{ is odd}}\\[12pt]24\sum \limits _{\begin{smallmatrix}m|n\\m{\text{ odd}}\end{smallmatrix}}m&{\text{if }}n{\text{ is even}}.\end{cases}}}$

Equivalently, it is eight times the sum of all its divisors which are not divisible by 4, i.e.

${\displaystyle r_{4}(n)=8\sum _{m\,:\,4\nmid m|n}m.}$

We may also write this as

${\displaystyle r_{4}(n)=8\sigma (n)-32\sigma (n/4)\ ,}$

where the second term is to be taken as zero if n is not divisible by 4. In particular, for a prime number p we have the explicit formula r4(p) = 8(p + 1).[10]

Some values of r4(n) occurs infinitely often as r4(n)=r4(2mn) whenever n is even. The values of r4(n)/n can be arbitrarily large: indeed, r4(n)/n is infinitely often larger than 8 √ log n.[11]

## Uniqueness

The sequence of positive integers which have only one representation as a sum of four squares (up to order) is:

1, 2, 3, 5, 6, 7, 8, 11, 14, 15, 23, 24, 32, 56, 96, 128, 224, 384, 512, 896 ... (sequence A006431 in OEIS).

These integers consist of the seven odd numbers 1, 3, 5, 7, 11, 15, 23 and all numbers of the form ${\displaystyle 2(4^{k}),6(4^{k})}$ or ${\displaystyle 14(4^{k})}$.

The sequence of positive integers which cannot be represented as a sum of four non-zero squares is:

1, 2, 3, 5, 6, 8, 9, 11, 14, 17, 24, 29, 32, 41, 56, 96, 128, 224, 384, 512, 896 ... (sequence A000534 in OEIS).

These integers consist of the eight odd numbers 1, 3, 5, 9, 11, 17, 29, 41 and all numbers of the form ${\displaystyle 2(4^{k}),6(4^{k})}$ or ${\displaystyle 14(4^{k})}$.