On this page
You can write 3 as 1 + 1 + 1 and 6 as 4 + 1 + 1, but try 7 and you run out of options. That is not bad luck. Legendre's three-square theorem says a natural number n can be written as x² + y² + z² with integers x, y, z if and only if n is not of the form 4^a(8b + 7), with a and b non-negative whole numbers (Legendre's three-square theorem, Wikipedia).
Which numbers cannot be written as a sum of three squares?
The first ones are 7, 15, 23, 28, 31, 39, 47, 55, 60, 63 and 71, catalogued as sequence A004215 in the OEIS. Each is 4^a(8b + 7): 7, 15, 23 and 31 have a = 0, while 28 = 4 × 7 and 60 = 4 × 15 have a = 1.
The history is tidy. The Wikipedia article records that N. Beguelin noticed in 1774 that every positive integer of neither the form 8n + 7 nor the form 4n is a sum of three squares, but gave no satisfactory proof, that Legendre obtained the first proof in 1797 or 1798, and that Gauss published a more general result in 1801 which contains it.
Why do 7, 15, 23 and 28 fail?
The easy half rests on one fact: modulo 8, every square is congruent to 0, 1 or 4 (Wikipedia). You can check it yourself. An odd number is 2k + 1, its square is 4k(k + 1) + 1, and k(k + 1) is even, so every odd square leaves remainder 1. Even squares leave 0 or 4.
Now add three remainders from {0, 1, 4} and list every possible total mod 8:
0, 1, 2, 3 (only 0s and 1s)
4, 5, 6 (one 4, the rest 0s and 1s)
0, 1 (two 4s, giving 8 or 9)
4 (three 4s, giving 12)
Seven never appears. So 7, 15, 23 and 31, which all leave remainder 7, cannot be sums of three squares.
28 needs one more step. Squares mod 4 are only 0 or 1, so if x² + y² + z² is a multiple of 4, all three squares must be 0 mod 4, which forces x, y and z to be even. Halve each one and you would have 28 ÷ 4 = 7 as a sum of three squares, which we just ruled out. The same halving works for every 4^a(8b + 7).
The other half, that every remaining number does work, is much harder. Dirichlet's classical proof from 1850 rests on quadratic reciprocity, his theorem on primes in arithmetic progressions and a fact about ternary quadratic forms (Wikipedia). If squares modulo a prime are new to you, start with our explainer on quadratic residues and the Legendre symbol.
Sums of three squares from 1 to 30
One representation for each number, with 0² allowed and left out. Check any line by adding:
1 = 1², 2 = 1² + 1², 3 = 1² + 1² + 1²
4 = 2², 5 = 2² + 1², 6 = 2² + 1² + 1²
7: none (remainder 7 mod 8)
8 = 2² + 2², 9 = 3², 10 = 3² + 1²
11 = 3² + 1² + 1², 12 = 2² + 2² + 2², 13 = 3² + 2², 14 = 3² + 2² + 1²
15: none (remainder 7 mod 8)
16 = 4², 17 = 4² + 1², 18 = 3² + 3², 19 = 3² + 3² + 1²
20 = 4² + 2², 21 = 4² + 2² + 1², 22 = 3² + 3² + 2²
23: none (remainder 7 mod 8)
24 = 4² + 2² + 2², 25 = 5², 26 = 5² + 1², 27 = 5² + 1² + 1²
28: none (4 × 7)
29 = 5² + 2², 30 = 5² + 2² + 1²
Exactly four of the thirty fail, matching the start of A004215.
How does it connect to Lagrange's four-square theorem?
Lagrange's four-square theorem, proved in 1770, says every natural number is a sum of four squares (Wikipedia). The same article notes Gauss's shortcut from three squares to four: any number that is 1 or 2 mod 4 is a sum of three squares, and a number not divisible by 4 reaches that form after subtracting 0 or 1. Take 7: 7 − 1 = 6 = 2² + 1² + 1², so 7 = 2² + 1² + 1² + 1².
Our post on Lagrange's four-square theorem gives the direct proof, and Fermat's rule for primes that are a sum of two squares is the first step towards the full two-square theorem. Together, the two-, three- and four-square theorems tell you how many squares any n needs.
How many ways can n be written as a sum of three squares?
Count ordered triples of integers, with order and signs mattering, and call the number r₃(n). For 11 the only pattern is 9 + 1 + 1. The 3 can sit in any of 3 places and each entry can be positive or negative, giving 3 × 8 = 24 triples. OEIS A005875 lists r₃(n), confirms r₃(11) = 24 and r₃(7) = 0, and notes that r₃(n) is zero only at the numbers in A004215.
Gauss found a formula for this count. For a squarefree n greater than 4, r₃(n) is 24h(−n) when n leaves remainder 3 mod 8, 0 when it leaves 7, and 12h(−4n) otherwise, where h is a class number (sum of squares function, Wikipedia). Class numbers lie beyond school maths, but notice that the zero for remainder 7 is built into the formula.
A five-minute practice set
Is 92 a sum of three squares? (92 = 4 × 23, and 23 leaves 7 mod 8: no.)
Is 112? (112 = 16 × 7: no.)
Is 104? (104 = 4 × 26, and 26 leaves 2 mod 8: yes, 104 = 10² + 2².)
Write 39 as a sum of four squares. (39 − 1 = 38 = 6² + 1² + 1², so 39 = 6² + 1² + 1² + 1².)
Find r₃(3). (Only 1 + 1 + 1, with 8 sign choices, so 8.)
If congruences still feel shaky, the Fermi Concept Library covers every concept in mathematics, from remainders to quadratic residues. When your own check of a case like 60 = 4 × 15 goes wrong, the Fermi Problem Library has a step-by-step solver that explains the reasoning, so you can see which step slipped.



