Lagrange's Four-Square Theorem Explained
Understand why every whole number is a sum of four squares, follow the proof in three moves, and see what happens with fewer squares.
Key takeaways
- Every nonnegative integer equals a² + b² + c² + d² for some integers a, b, c and d.
- An identity of Euler shows that a product of sums of four squares is again such a sum, which reduces the problem to primes.
- For a prime, one first represents some small multiple of it and then drives the multiplier down to 1.
- Three squares suffice exactly when the number is not of the form 4^m(8k + 7).
Lagrange's four-square theorem says that every nonnegative integer is a sum of four squares of integers, where zeros are allowed. For example, 7 = 2² + 1² + 1² + 1², and 30 = 5² + 2² + 1² + 0².
Four is the smallest number that works for everything: 7 cannot be written with three squares. Joseph-Louis Lagrange published the first complete proof in 1770, and its outline is accessible to anyone comfortable with remainders and a little algebra.
Trying it out
| n | one representation | squares needed |
|---|---|---|
| 5 | 4 + 1 | 2 |
| 6 | 4 + 1 + 1 | 3 |
| 7 | 4 + 1 + 1 + 1 | 4 |
| 15 | 9 + 4 + 1 + 1 | 4 |
| 23 | 9 + 9 + 4 + 1 | 4 |
| 50 | 49 + 1 | 2 |
Beware of the greedy approach, which always subtracts the largest square that fits. For 23 it takes 16, then 4, then 1, 1 and 1, using five squares. A better first choice, 9, leads to success. Existence does not mean that the obvious method finds it, a lesson that applies to greedy algorithms in general.
History
The statement appears to be implicit in Diophantus, and Claude Gaspard Bachet stated it explicitly in his 1621 edition of the Arithmetica. Fermat said he had a proof but did not publish it. Euler worked on the problem for many years and found the key identity and other pieces. Lagrange completed the argument, and Euler then simplified it.
This sequence is typical of long-open problems: partial tools accumulate until someone fits them together.
Move one: reduce to primes
Euler's identity writes the product (a² + b² + c² + d²)(e² + f² + g² + h²) as the sum of the squares of
ae − bf − cg − dh
af + be + ch − dg
ag − bh + ce + df
ah + bg − cf + de Expanding confirms it: all the cross terms cancel. With (2, 1, 1, 1) for 7 and (3, 2, 1, 1) for 15, the four expressions are 2, 7, 6 and 4, and 4 + 49 + 36 + 16 = 105 = 7 × 15.
Since 1 and 2 are clearly sums of four squares, and every larger integer is a product of primes, it suffices to handle odd primes. The identity is the multiplication rule for lengths of quaternions, discovered by Hamilton long after Euler wrote the formula down.
Move two: catch a multiple of p
For an odd prime p, find integers a and b with a² + b² + 1 ≡ 0 (mod p).
The values of a², for a from 0 to (p − 1)/2, are all different modulo p, giving (p + 1)/2 remainders. The values of −1 − b² over the same range give another (p + 1)/2. Together that is p + 1 values in only p classes, so the two lists overlap.
For p = 7, the squares give 0, 1, 4 and 2. The expressions −1 − b² give 6, 5, 2 and 4. The value 2 appears in both: a = 3 and b = 2. Indeed 3² + 2² + 1² = 14 = 2 × 7. So some multiple mp, with m smaller than p, is a sum of four squares.
Move three: shrink the multiplier
Take the smallest positive m for which mp is a sum of four squares, and suppose m > 1.
If m is even, an even number of the four terms are odd, so you can pair them into couples of equal parity. Then ((a + b)/2)² + ((a − b)/2)² + ((c + d)/2)² + ((c − d)/2)² = mp/2, a smaller multiple.
For 14 = 3² + 1² + 2² + 0², this yields 2² + 1² + 1² + 1² = 7.
If m is odd and greater than 1, replace each term by its remainder modulo m of smallest absolute value, so each has size below m/2. The sum of the new squares is a multiple of m, say mn, with n smaller than m. And n is not zero, since that would force m² to divide mp, which cannot happen when m lies strictly between 1 and p.
Now reverse the signs of the last three remainders and multiply the two representations with Euler's identity. Every one of the four resulting terms is divisible by m. The product equals m²np, so dividing each term by m leaves np as a sum of four squares. That contradicts the choice of m. So m = 1.
This is a form of Fermat's method of descent.
A descent from start to finish
Follow the whole method for p = 11. The values a = 3 and b = 1 give 9 + 1 + 1 = 11, so m = 1 immediately. A luckier or unluckier choice can start higher: 33 = 4² + 4² + 1² + 0² represents 3 × 11, with m = 3.
Reduce 4, 4, 1 and 0 modulo 3 to get 1, 1, 1 and 0. Their squares sum to 3 = 3 × 1, so n = 1. Reverse the last three signs to obtain (1, −1, −1, 0) and apply the identity to (4, 4, 1, 0).
first 4·1 − 4·(−1) − 1·(−1) − 0 = 9
second 4·(−1) + 4·1 + 1·0 − 0·(−1) = 0
third 4·(−1) − 4·0 + 1·1 + 0 = −3
fourth 4·0 + 4·(−1) − 1·(−1) + 0 = −3 All four are divisible by 3. Dividing gives 3, 0, −1 and −1, and 9 + 0 + 1 + 1 = 11. The multiplier has dropped from 3 to 1 in a single step.
Finding representations by hand
For a number of modest size, work downward from the largest square.
- If the number is divisible by 4, represent a quarter of it and double each term.
- Check whether it leaves remainder 7 modulo 8. If so, all four squares are needed.
- Subtract a square a², starting with the largest, and test whether the remainder is a sum of three squares.
- Represent that remainder by subtracting another square and testing for two squares.
Take 310. It leaves remainder 6 modulo 8, so three squares will do. Subtract 17² = 289 to get 21, which leaves remainder 5 and so is a sum of three squares: 16 + 4 + 1. Hence 310 = 17² + 4² + 2² + 1².
Now take 448 = 64 × 7. Three factors of 4 can be removed, leaving 7 = 2² + 1² + 1² + 1². Multiply each term by 8 to get 448 = 16² + 8² + 8² + 8².
Why three are not enough
Squares leave remainders 0, 1 or 4 modulo 8. No three of these add to 7 modulo 8, so numbers such as 7, 15, 23 and 31 need four squares.
Multiplying by 4 does not help. If 4n is a sum of three squares, all three must be even, so n is too. Hence 28 and 112 also need four. Legendre's three-square theorem, with a complete treatment by Gauss, says these are the only exceptions. The two-square case is described in sums of two squares.
How many ways?
Carl Jacobi found that the number of integer solutions of a² + b² + c² + d² = n, counting order and signs, equals 8 times the sum of the divisors of n that are not divisible by 4.
For n = 1 this gives 8, namely the four positions for ±1. For n = 4 the eligible divisors are 1 and 2, giving 24: sixteen solutions with every term ±1, and eight with a single ±2.
For an odd prime p the count is 8(p + 1), so large primes have many representations. The proof usually goes through theta series, a bridge between counting and complex analysis.
Why dimension four is special
Four is the first number of variables at which all local obstructions disappear for sums of squares. With two squares, many primes are excluded. With three, one family modulo 8 is excluded, along with its multiples by powers of 4. With four, the remainder test never fails modulo any number.
The algebra helps too. Products of sums of squares are again sums of the same number of squares in 1, 2, 4 and 8 variables, matching the real numbers, complex numbers, quaternions and octonions. No such identity exists for three squares. You can see the failure in numbers: 3 = 1 + 1 + 1 and 5 = 4 + 1 + 0 are sums of three squares, but their product 15 is not.
A proof of the theorem can also be given with the geometry of numbers, by showing that a suitable region in four-dimensional space is large enough to contain a nonzero point of a particular lattice. Minkowski's theorem supplies that point.
Other forms that represent everything
Which other expressions of the shape ax² + by² + cz² + dw² represent all positive integers? Ramanujan studied this and listed dozens; there are 54 such diagonal forms when order is ignored.
In the 1990s John Conway and William Schneeberger announced the fifteen theorem: a positive definite quadratic form with an integer matrix represents every positive integer if it represents 1, 2, 3, 5, 6, 7, 10, 14 and 15. Manjul Bhargava gave a streamlined proof, and he and Jonathan Hanke later proved a companion criterion for all integer-valued forms, which involves checking up to 290.
Going to higher powers
Edward Waring asked in 1770 whether a fixed number of cubes, or of fourth powers, would always suffice. That question opened a long line of research, told in Waring's problem.
What to take from the proof
The argument is a model worth remembering, because its pattern returns throughout number theory. First, find an identity that lets you assemble general cases from prime ones. Second, show that something close to what you want exists, here a multiple of p, usually by a counting argument. Third, improve that approximate success step by step until it is exact.
Each move uses a different kind of reasoning: algebra, the pigeonhole principle and descent. None is deep on its own. The difficulty that occupied Euler for so long lay in finding the right combination. If you want to test your understanding, run all three moves for p = 19 on paper, starting by searching for a and b with a² + b² + 1 divisible by 19.
Common questions
Do the four squares have to be different?
No. Repeats and zeros are allowed. If you demand four positive squares, some numbers fail, such as 5 and 8.
Is there a fast way to find the squares for a large number?
Yes. Randomized algorithms due to Michael Rabin and Jeffrey Shallit find a representation quickly, without needing to factor the number in the ordinary sense. Trial and error is fine for small cases.
Do negative numbers work?
No. A sum of squares of real numbers is never negative.