Legendre's Three-Square Theorem

lean4-proofnumber-theoryvisualization
n=a2+b2+c2    n4a(8b+7)n = a^2 + b^2 + c^2 \iff n \neq 4^a(8b+7)

Statement

Legendre's three-square theorem (1798): a positive integer nn is representable as a sum of three perfect squares,

n=a2+b2+c2,a,b,cZ,n = a^2 + b^2 + c^2, \qquad a, b, c \in \mathbb{Z},

if and only if nn is not of the form 4a(8b+7)4^a(8b + 7) for non-negative integers aa and bb.

The excluded residues mod 88 that cannot be sums of three squares are {7}\{7\}. Multiplying by 44 propagates the obstruction: if nn is excluded, so is 4n4n.

Visualization

Status for n=1n = 1 through 3030. Excluded numbers (4a(8b+7)4^a(8b+7)) are marked with ×\times:

nnForm?StatusRepresentation
78(0)+78(0)+7×\timesno three-square rep
14OK12+22+32=141^2+2^2+3^2=14
158(1)+78(1)+7×\timesno three-square rep
218(2)+58(2)+5OK12+22+42=211^2+2^2+4^2=21
238(2)+78(2)+7×\timesno three-square rep
28474\cdot7×\timesno three-square rep
30OK12+22+52=301^2+2^2+5^2=30

Complete list of excluded n30n \le 30: {7,15,23,28}\{7, 15, 23, 28\}.

All other n30n \le 30 admit a representation. For instance:

  • n=5n = 5: 22+12+02=52^2 + 1^2 + 0^2 = 5
  • n=12n = 12: 22+22+22=122^2 + 2^2 + 2^2 = 12
  • n=30n = 30: 12+22+52=301^2 + 2^2 + 5^2 = 30

Proof Sketch

  1. Necessity (excluded form cannot be represented). Any square is 0,1,\equiv 0, 1, or 4(mod8)4 \pmod 8, so the sum of three squares is 0,1,2,3,4,5,6(mod8)\equiv 0,1,2,3,4,5,6\pmod 8 but never 77. Hence 8b+78b+7 is excluded. The factor-of-44 obstruction follows: if a2+b2+c2=4na^2+b^2+c^2 = 4n, then a,b,ca, b, c are all even (check mod 4), so (a/2)2+(b/2)2+(c/2)2=n(a/2)^2+(b/2)^2+(c/2)^2 = n.
  2. Sufficiency (all other nn are representable). The proof uses Dirichlet's theorem on primes in arithmetic progressions to find a prime p3(mod4)p \equiv 3 \pmod 4 that divides a suitable form, then reduces to showing primes not of the excluded form are representable.
  3. Reduction to primes. For each prime p≢7(mod8)p \not\equiv 7 \pmod 8 one constructs a direct representation; the general case uses composition identities.

Connections

This is the three-square analogue of Lagrange's Four-Square TheoremLagrange's Four-Square Theoremn=a2+b2+c2+d2n = a^2 + b^2 + c^2 + d^2Every natural number is the sum of four perfect squaresRead more →, which has no exceptions. The excluded residue condition is a local obstruction at the prime 22, similar in spirit to the local-global principle underlying Sum of Two SquaresSum of Two Squaresp=a2+b2    p=2 or p1(mod4)p = a^2 + b^2 \iff p = 2 \text{ or } p \equiv 1 \pmod{4}A prime is a sum of two perfect squares if and only if it equals 2 or is congruent to 1 mod 4Read more → (Fermat's characterisation via primes 1(mod4)\equiv 1 \pmod 4).

Lean4 Proof

import Mathlib.NumberTheory.SumFourSquares

/-- Explicit three-square witness: 30 = 1² + 2² + 5². -/
theorem three_sq_30 :  a b c : , a ^ 2 + b ^ 2 + c ^ 2 = 30 :=
  1, 2, 5, by norm_num

/-- Explicit three-square witness: 14 = 1² + 2² + 3². -/
theorem three_sq_14 :  a b c : , a ^ 2 + b ^ 2 + c ^ 2 = 14 :=
  1, 2, 3, by norm_num

/-- The excluded form 4^0*(8*0+7) = 7 is indeed 7 mod 8. -/
theorem seven_mod_eight : 7 % 8 = 7 := by decide

/-- The mod-8 residues of squares: decide on all residue classes 0..7. -/
theorem sq_mod_eight_set :  r : Fin 8, (r.val ^ 2) % 8 = 0 
    (r.val ^ 2) % 8 = 1  (r.val ^ 2) % 8 = 4 := by decide