On this page
Write numbers as sums of squares and a pattern appears fast: 5 needs two (4 + 1), 6 needs three, and 7 needs four. No whole number needs five. That is a theorem, and you can check most of it with pencil and paper.
Can every whole number be written as a sum of four squares?
Yes. Lagrange's four-square theorem states that every non-negative integer can be written as a² + b² + c² + d², where a, b, c and d are integers. Zeros are allowed, so 3 = 1² + 1² + 1² + 0²; "four squares" means "at most four non-zero squares".
Joseph-Louis Lagrange published the first proof in 1770; Euler was unable to prove it, notes Wolfram MathWorld. The claim is older: Claude Gaspard Bachet stated it in his 1621 Latin translation of Diophantus's Arithmetica, so it is also called Bachet's conjecture (Wikipedia).
What do the numbers from 1 to 30 look like?
The fewest squares each number up to 30 needs, found by trying every combination of 1² to 5²:
One square: 1, 4, 9, 16, 25.
Two squares: 2, 5, 8, 10, 13, 17, 18, 20, 26, 29 (for example 18 = 3² + 3² and 29 = 5² + 2²).
Three squares: 3, 6, 11, 12, 14, 19, 21, 22, 24, 27, 30 (for example 12 = 2² + 2² + 2² and 30 = 5² + 2² + 1²).
Four squares: 7 = 2² + 1² + 1² + 1², 15 = 3² + 2² + 1² + 1², 23 = 3² + 3² + 2² + 1², 28 = 5² + 1² + 1² + 1².
Only four of the thirty need all four. They open OEIS sequence A004215, "numbers that are the sum of 4 but no fewer nonzero squares": 7, 15, 23, 28, 31, 39, 47 and on.
Why do 7, 15 and 28 need all four squares?
Legendre's three-square theorem says a natural number is a sum of three squares if and only if it is not of the form 4ᵃ(8b + 7), with a and b non-negative integers. You can prove the "not three squares" half yourself.
Every square leaves remainder 0, 1 or 4 when divided by 8 (odd squares 1, 9, 25, 49 all leave 1).
Add any three of 0, 1 and 4 and the remainder mod 8 is never 7. So 7, 15, 23 and every number of the form 8b + 7 cannot be three squares.
For 28 = 4 × 7: squares leave 0 or 1 mod 4, so three squares make a multiple of 4 only if all three are even. Halve each and you get three squares adding to 7, ruled out above. The same halving handles 60, 112 and the rest.
Try it on 31, 39 and 60: all three need four squares, but 61 = 6² + 5².
How does Euler's four-square identity help prove it?
Euler's four-square identity says that the product of two sums of four squares is again a sum of four squares. Euler wrote about it in a letter to Christian Goldbach dated 4 May 1748, and Lagrange used it so that the theorem only had to be proved for primes, since every whole number above 1 is a product of primes (here is why 1 is left out of the primes).
A worked example: 7 × 3 = 21. Take 7 = 2² + 1² + 1² + 1² and 3 = 1² + 1² + 1² + 0², so (a₁, a₂, a₃, a₄) = (2, 1, 1, 1) and (b₁, b₂, b₃, b₄) = (1, 1, 1, 0). The identity's first term is a₁b₁ − a₂b₂ − a₃b₃ − a₄b₄ = 2 − 1 − 1 − 0 = 0; the other three come out as 2, 4 and 1. So 21 = 0² + 2² + 4² + 1² = 16 + 4 + 1.
For each odd prime p, the classical proof uses the pigeonhole principle to find a and b with a² + b² + 1 a multiple of p, then shrinks that multiple step by step until it reaches 1. Both steps run on squares taken modulo a prime, the subject of our guide to quadratic residues and the Legendre symbol.
How does this build on the two-square theorem?
Two squares are not enough for every number: 3, 6 and 7 all fail. Which primes are a sum of two squares is the subject of our post on Fermat's two-square theorem.
The two-square story has its own product rule, the Brahmagupta–Fibonacci identity, and Euler's identity does for quaternions what that rule does for complex numbers. Hurwitz's theorem, on the same page, says a product rule of this kind exists only for 1, 2, 4 or 8 squares. The plan is the same at both levels: settle the primes, then multiply.
If the remainder arguments feel shaky, the Fermi Concept Library covers the number theory behind them, and the Fermi Problem Library has a step-by-step solver that explains the reasoning, to set beside your own mod 8 argument.



