Lagrange's Four-Square Theorem

lean4-proofnumber-theoryvisualization
n=a2+b2+c2+d2n = a^2 + b^2 + c^2 + d^2

Statement

Lagrange's four-square theorem (1770): every non-negative integer nn can be expressed as the sum of four perfect squares:

n=a2+b2+c2+d2,a,b,c,dN.n = a^2 + b^2 + c^2 + d^2, \qquad a, b, c, d \in \mathbb{N}.

Note that 02=00^2 = 0 is allowed, so the four squares need not be positive. By contrast, not every integer is a sum of three squares (see Legendre's Three-Square TheoremLegendre's Three-Square Theoremn=a2+b2+c2    n4a(8b+7)n = a^2 + b^2 + c^2 \iff n \neq 4^a(8b+7)A positive integer is a sum of three squares iff it is not of the form 4^a(8b+7)Read more →).

Visualization

Representations of n=1n = 1 through 1010 as sums of four squares (minimal number of non-zero terms where possible):

nnRepresentation
112+02+02+021^2 + 0^2 + 0^2 + 0^2
212+12+02+021^2 + 1^2 + 0^2 + 0^2
312+12+12+021^2 + 1^2 + 1^2 + 0^2
422+02+02+022^2 + 0^2 + 0^2 + 0^2
522+12+02+022^2 + 1^2 + 0^2 + 0^2
622+12+12+022^2 + 1^2 + 1^2 + 0^2
722+12+12+122^2 + 1^2 + 1^2 + 1^2
822+22+02+022^2 + 2^2 + 0^2 + 0^2
932+02+02+023^2 + 0^2 + 0^2 + 0^2
1032+12+02+023^2 + 1^2 + 0^2 + 0^2

The number 77 is the smallest positive integer that requires all four squares to be non-zero.

Proof Sketch

  1. Euler's four-square identity. The product of two sums of four squares is again a sum of four squares:
(a2+b2+c2+d2)(e2+f2+g2+h2)=(Hurwitz quaternion norm product).(a^2+b^2+c^2+d^2)(e^2+f^2+g^2+h^2) = (\text{Hurwitz quaternion norm product}).

This reduces the theorem to prime numbers. 2. Every prime is a sum of four squares. For p=2p = 2: 2=1+1+0+02 = 1+1+0+0. For an odd prime pp: show that 1-1 is a sum of two squares mod pp, then apply the pigeonhole principle to find a2+b2+10(modp)a^2 + b^2 + 1 \equiv 0 \pmod{p} with a,b<p/2|a|, |b| < p/2. This gives a2+b2+12+02=kpa^2 + b^2 + 1^2 + 0^2 = kp for some 1k<p1 \le k < p. A descent argument reduces kk to 11. 3. Every integer is a product of primes (by the Fundamental Theorem of ArithmeticFundamental Theorem of Arithmeticn=p1a1p2a2pkakn = p_1^{a_1} \cdot p_2^{a_2} \cdots p_k^{a_k}Every integer greater than 1 has a unique prime factorizationRead more →), so Euler's identity propagates the four-square property.

Connections

This theorem generalises 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 →, which characterises exactly which integers are sums of two squares. The proof technique via quaternion norms is a precursor to the Cayley–Dickson construction, also appearing in Cauchy–Schwarz InequalityCauchy–Schwarz Inequalityu,vuv|\langle u, v \rangle| \leq \|u\| \, \|v\|The inner product of two vectors is bounded by the product of their normsRead more → proofs via inner products.

Lean4 Proof

import Mathlib.NumberTheory.SumFourSquares

/-- Lagrange's four-square theorem: every natural number is a sum of four squares.
    Direct alias of `Nat.sum_four_squares` in Mathlib. -/
theorem lagrange (n : ) :  a b c d : , a ^ 2 + b ^ 2 + c ^ 2 + d ^ 2 = n :=
  Nat.sum_four_squares n

/-- Explicit witness: 7 = 2² + 1² + 1² + 1². -/
theorem four_sq_7 :  a b c d : , a ^ 2 + b ^ 2 + c ^ 2 + d ^ 2 = 7 :=
  2, 1, 1, 1, by norm_num