Take any positive whole number and try to write it as a sum of perfect squares, $0, 1, 4, 9, 16, 25, \ldots$, using as few terms as possible:

$$\begin{aligned} 5&=4+1,\\ 6&=4+1+1,\\ 7&=4+1+1+1,\\ 30&=25+4+1,\\ 2026&=2025+1=45^2+1^2. \end{aligned}$$

Some numbers are squares, others need two, others three. The number 7 needs four: there is no other way. Could some number need five? In 1770 Lagrange proved that no number does:

Every positive whole number is a sum of four squares of integers.

Counting zero as a square, this means there are always $a, b, c, d$ with $n=a^2+b^2+c^2+d^2$.

A suspicion fifteen centuries old

The Greek Diophantus of Alexandria, around the year 250, uses in his problems, without comment, the fact that certain numbers can be written as a sum of four squares. In 1621 the Frenchman Claude-Gaspard Bachet published a Latin edition of Diophantus’s Arithmetica and stated the claim explicitly. He said he had checked it up to 325, but did not know how to prove it.

It was in the margins of Bachet’s book that Pierre de Fermat wrote his famous notes. Fermat declared he had a proof by his method of infinite descent but, as usual, never showed it.

Euler’s forty years

Euler tried to prove it for about forty years and never finished. But in 1748 he found the main piece: an identity showing that the product of two sums of four squares is again a sum of four squares.

$$\begin{aligned} &(a^2+b^2+c^2+d^2)(e^2+f^2+g^2+h^2)\\ &\quad=(ae+bf+cg+dh)^2\\ &\qquad+(af-be+ch-dg)^2\\ &\qquad+(ag-bh-ce+df)^2\\ &\qquad+(ah+bg-cf-de)^2. \end{aligned}$$

It looks like magic, but you only need to expand both sides: all the cross products cancel in pairs. For example, $7=2^2+1^2+1^2+1^2$ and $15=3^2+2^2+1^2+1^2$. The identity gives

$$\begin{aligned}105&=7\cdot15=10^2+1^2+0^2+(-2)^2\\&=100+1+0+4.\end{aligned}$$

The consequence is powerful. Every number is a product of primes. If each prime is a sum of four squares, the identity guarantees that the products are too. So it is enough to prove the theorem for primes.

Lagrange’s proof

That is where Lagrange came in, in 1770, already in Berlin. His proof has two stages, and both belong to the family of Fermat’s descent.

First stage. For every odd prime $p$, there exist $x$ and $y$ such that $x^2+y^2+1$ is a multiple of $p$. The reason is a count: the numbers $x^2$, with $x$ from $0$ to $\frac{p-1}2$, leave $\frac{p+1}2$ different remainders on division by $p$, and so do the numbers $-1-y^2$. Since $\frac{p+1}2+\frac{p+1}2=p+1$ is more than the $p$ possible remainders, by the pigeonhole principle one of the lists must repeat a remainder of the other. So some multiple of $p$, say $mp$, is a sum of four squares: $x^2+y^2+1^2+0^2$.

Second stage. If $mp$ is a sum of four squares with $m>1$, Lagrange shows, using Euler’s identity, that a smaller multiple $m'p$, with $m'

Euler read the proof and, shortly afterwards, published a simpler version, acknowledging Lagrange’s merit. The two corrected and completed each other by letter, as they had done with the calculus of variations.

Who really needs four?

The number 7 needs four squares, and so do 15, 23, 28 and 31. In 1798 Legendre found exactly which ones they are, and Gauss completed the proof in 1801: the numbers of the form

$$4^a\,(8b+7),$$

and only these, are not sums of three squares. All the others manage with three or fewer.

Exercise: write 2023 as a sum of four squares

Notice that $2023=7\cdot17^2$ and that $7=2^2+1^2+1^2+1^2$. Multiplying everything by $17^2$,

$$2023=34^2+17^2+17^2+17^2=1156+289+289+289.$$

There are other answers, for example $43^2+13^2+2^2+1^2$. Since $2023=8\cdot252+7$, it has Legendre’s form: three squares will not do.

Exercise: why is 7 not a sum of three squares?

Look at the remainders on division by 8. A square can only leave remainder $0$, $1$ or $4$: for example, $1, 4, 9, 16, 25, 36, 49$ leave $1, 4, 1, 0, 1, 4, 1$. Adding three such remainders, the possibilities are $0, 1, 2, 3, 4, 5, 6$ (like $4+1+1$) and never $7$. So no number that leaves remainder 7 on division by 8 is a sum of three squares. This is the case $a=0$ of Legendre’s rule.

Sources: biography of Lagrange, MacTutor · André Weil, Number Theory: An Approach through History from Hammurapi to Legendre (1984) · Leonard E. Dickson, History of the Theory of Numbers, vol. 2 (1920).


All Lagrange topics · ← The Lagrange points: where gravity balances · Shuffling the roots: Lagrange’s theorem →

Image credits.