Sums of Four Squares

Take any positive integer you like — say 7, or 2023, or a billion. Ask whether it can be written as a sum of four perfect squares (zero allowed). Try 7: it is 4+1+1+1=22+12+12+124+1+1+1 = 2^2+1^2+1^2+1^2. Try 2023: it is 2023=442+62+12+122023 = 44^2 + 6^2 + 1^2+1^2. This is no accident. Lagrange's four-square theorem asserts that every positive integer is a sum of four squares. There are no exceptions and no extra hypotheses. This is a striking contrast to sums of two squares. There, Sums of Two Squares shows that primes p≡3(mod4)p\equiv 3\pmod 4 must appear to an even power. The result matters because it is the cleanest possible universality statement in the theory of quadratic forms: the form x2+y2+z2+w2x^2+y^2+z^2+w^2 represents everything. It sits at the headwaters of the study of which integers a given quadratic form can represent. This subject blossoms into the full theory of Binary Quadratic Forms. It extends beyond into the theory of quadratic forms in nn variables and modular forms. Historically it was conjectured by Bachet (from Diophantus) and Fermat. It was finally proved by Lagrange in 1770 using a descent argument sharpened by an identity of Euler. The big picture: three squares are not enough (77 is not a sum of three squares). But four is exactly the right number to close off all obstructions. The proof has two independent engines. First, an algebraic identity — Euler's four-square identity — reduces the whole problem to primes. If every prime is a sum of four squares, a multiplicativity argument extends this to every product of primes. Hence it extends to every integer greater than 11. (And 1=12+02+02+021=1^2+0^2+0^2+0^2, 0=02+02+02+020 = 0^2+0^2+0^2+0^2 trivially.) Second, a beautiful pigeonhole-and-descent argument shows every odd prime is a sum of four squares. This argument is due to Lagrange and was streamlined by later writers. This article gives the full, self-contained proof. It covers the identity, the key lemma, and the descent step. The key lemma states some multiple mpmp of a prime (0<m<p0<m<p) is a sum of four squares. The infinite-descent step shrinks mm down to 11. We verify every computation. We discuss why each hypothesis (four squares, not three) is essential. We close with the historical and modern context — a gateway to Waring's problem and the theory of quadratic forms.

Key takeaways
  • Statement: every non-negative integer nn can be written as n=a2+b2+c2+d2n = a^2+b^2+c^2+d^2 with a,b,c,d∈Z≥0a,b,c,d\in\mathbb Z_{\ge 0}.
  • Reduction step: Euler's four-square identity shows the set of sums of four squares is closed under multiplication.
  • This reduces the theorem to proving it for primes only.
  • Core step: for any odd prime pp, some 0<m<p0<m<p makes mpmp a sum of four squares (pigeonhole on residues).
  • Descent step: if m>1m>1 divides a representable mpmp, one can always produce a smaller multiple of pp that is also a sum of four squares, forcing m=1m=1 eventually.
  • Key idea: four squares are exactly enough because a quaternion-style identity makes "sum of four squares" a multiplicatively closed property, and infinite descent kills every obstruction prime by prime.

Where this lives today

The four-square theorem is the prototype for Waring's problem: for each kk, does every sufficiently large integer decompose as a bounded sum of kk-th powers? Lagrange's theorem is the case k=2k=2 with the exact bound 44 established (unlike higher Waring exponents, where the minimal number of summands is much harder to pin down and, for some kk, still not exactly known in full generality). In the theory of quadratic forms, x2+y2+z2+w2x^2+y^2+z^2+w^2 is the simplest positive-definite quaternary form representing all non-negative integers; the classification of which quadratic forms are "universal" (represent everything) is a rich modern subject, culminating in results such as the Conway–Schneeberger "15 theorem" characterizing universal quaternary forms by checking representability of integers up to 1515. This connects directly onward to Binary Quadratic Forms, where the two-variable analogue of representability questions is governed by class numbers and reduction theory, and to the broader theory of modular forms, where theta series built from quadratic forms (such as θ(z)=∑n∈Z4qn12+n22+n32+n42\theta(z) = \sum_{n\in\mathbb Z^4} q^{n_1^2+n_2^2+n_3^2+n_4^2}) encode representation counts as Fourier coefficients — Jacobi's formula for r4(n)r_4(n) is exactly the statement that this theta series is a specific weight-2 modular form. In algebra, the identity underlying the proof is a shadow of the multiplicativity of the norm form on the quaternions H\mathbb{H}, the first genuinely noncommutative division algebra over R\mathbb{R}; this is the same structural fact exploited in the two-square case via Gaussian integers (norms in Z[i]\mathbb{Z}[i]) as covered in Sums of Two Squares, and it generalizes further to Cayley's eight-square identity over the octonions. In computer science and cryptography, algorithms for finding an explicit four-square decomposition of a given integer nn run in polynomial time (via a randomized reduction to sums of two squares over primes dividing auxiliary quantities), and are used in some lattice-based and combinatorial constructions; the underlying pigeonhole-plus-descent technique is a recurring pattern in constructive existence proofs across combinatorics and coding theory. In statistical mechanics and physics, sums of squares appear naturally as energies of lattice vectors, and representation-counting functions like r4(n)r_4(n) arise in computing degeneracies of quantum states on four-dimensional lattices.

Ask the tutor: Can you walk through why the descent step needs mm to be odd, using a fresh numerical example where mm starts even?

DefinitionSum of four squares

A non-negative integer nn is a sum of four squares if there exist integers a,b,c,da,b,c,d (not necessarily distinct or positive, zero permitted) such that

n=a2+b2+c2+d2.n = a^2+b^2+c^2+d^2.

Equivalently, nn lies in the image of the quaternary quadratic form Q(a,b,c,d)=a2+b2+c2+d2Q(a,b,c,d)=a^2+b^2+c^2+d^2 on Z4\mathbb{Z}^4.

Intuition

Sums of two squares are picky: 33, 66, 77, 1111, 1212 are all not sums of two squares, and Sums of Two Squares characterizes exactly which nn qualify, via the exponents of primes ≡3(mod4)\equiv 3 \pmod 4 in their factorization. Sums of three squares are also picky — Legendre's three-square theorem says nn fails exactly when n=4a(8k+7)n = 4^a(8k+7). The remarkable phenomenon with four squares is that the pickiness vanishes entirely: no number is excluded. Why does adding one more square variable remove every obstruction? Heuristically, the "local" obstructions to two- and three-square representability come from congruence conditions modulo 44 and 88: squares are always ≡0,1(mod4)\equiv 0,1\pmod 4, so sums of two or three squares miss certain residues completely. With four squares, every residue mod 8 becomes achievable, and — more deeply — the multiplicative identity below shows that the set of representable numbers is closed under multiplication in a way that is robust enough to absorb every prime, without leaving any congruence-class gap. The picture below shows how three squares fail to reach 77 but four squares reach it immediately, and how the key identity "multiplies" representations together.

Figure
Three squares miss 7; four squares always land exactly on target
Sums of squares ≤ 12 on the integer line 012 345 678 91011 12 3 squares: reach all but 7 7 unreachable 4 squares: every integer 0..12 is reached 7 = 4+1+1+1

Central theorem

TheoremLagrange's Four-Square Theorem

Every non-negative integer nn can be written as a sum of four squares of integers:

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

We isolate the machinery into named lemmas, each with hypotheses stated explicitly, and assemble them into the final proof.

LemmaEuler's Four-Square Identity

For all real (in particular integer) numbers a1,a2,a3,a4,b1,b2,b3,b4a_1,a_2,a_3,a_4,b_1,b_2,b_3,b_4,

(a12+a22+a32+a42)(b12+b22+b32+b42)=c12+c22+c32+c42,(a_1^2+a_2^2+a_3^2+a_4^2)(b_1^2+b_2^2+b_3^2+b_4^2) = c_1^2+c_2^2+c_3^2+c_4^2,

where

c1=a1b1+a2b2+a3b3+a4b4,c2=a1b2−a2b1+a3b4−a4b3,c3=a1b3−a2b4−a3b1+a4b2,c4=a1b4+a2b3−a3b2−a4b1.\begin{aligned} c_1 &= a_1b_1+a_2b_2+a_3b_3+a_4b_4,\\ c_2 &= a_1b_2-a_2b_1+a_3b_4-a_4b_3,\\ c_3 &= a_1b_3-a_2b_4-a_3b_1+a_4b_2,\\ c_4 &= a_1b_4+a_2b_3-a_3b_2-a_4b_1. \end{aligned}
Proof

This is a direct algebraic verification (technique: brute-force expansion, made transparent via quaternion multiplication). Consider the quaternions

α=a1+a2i+a3j+a4k,β=b1+b2i+b3j+b4k\alpha = a_1+a_2i+a_3j+a_4k,\qquad \beta = b_1+b_2i+b_3j+b_4k

in the quaternion algebra H\mathbb{H} with i2=j2=k2=−1i^2=j^2=k^2=-1, ij=k=−jiij=k=-ji, jk=i=−kjjk=i=-kj, ki=j=−ikki=j=-ik. For a quaternion α=a1+a2i+a3j+a4k\alpha=a_1+a_2i+a_3j+a_4k define its conjugate αˉ=a1−a2i−a3j−a4k\bar\alpha = a_1-a_2i-a_3j-a_4k and its norm N(α)=ααˉ=a12+a22+a32+a42N(\alpha)=\alpha\bar\alpha = a_1^2+a_2^2+a_3^2+a_4^2, which is a nonnegative real scalar (the cross terms cancel because i,j,ki,j,k anticommute pairwise and each squares to −1-1; this is a direct computation from the multiplication rules above). Quaternion multiplication is associative (it is a specific instance of an associative algebra defined by bilinear extension of the rules on basis elements 1,i,j,k1,i,j,k; associativity is checked once on the 4×4×4 table of basis products and then follows for all quaternions by bilinearity). Conjugation is an anti-automorphism: αβ‾=βˉ αˉ\overline{\alpha\beta} = \bar\beta\,\bar\alpha, verified directly from the defining rules by checking it on 1,i,j,k1,i,j,k and extending bilinearly. Hence for any quaternions α,β\alpha,\beta,

N(αβ)=(αβ)(αβ)‾=αββˉαˉ=α N(β) αˉ=N(β) ααˉ=N(α)N(β),N(\alpha\beta) = (\alpha\beta)\overline{(\alpha\beta)} = \alpha\beta\bar\beta\bar\alpha = \alpha\, N(\beta)\,\bar\alpha = N(\beta)\,\alpha\bar\alpha = N(\alpha)N(\beta),

using that N(β)N(\beta) is a real scalar and hence commutes with α\alpha. This proves N(α)N(β)=N(αβ)N(\alpha)N(\beta) = N(\alpha\beta). Now compute αβ\alpha\beta explicitly using the multiplication table

i2=j2=k2=−1,ij=−ji=k,jk=−kj=i,ki=−ik=j,i^2=j^2=k^2=-1,\quad ij=-ji=k,\quad jk=-kj=i,\quad ki=-ik=j,

and collect the coefficient of 1,i,j,k1, i, j, k in

αβ=(a1+a2i+a3j+a4k)(b1+b2i+b3j+b4k).\alpha\beta = (a_1+a_2i+a_3j+a_4k)(b_1+b_2i+b_3j+b_4k).

Expanding term by term (16 products) and grouping by basis element gives exactly

αβ=c1+c2i+c3j+c4k\alpha\beta = c_1 + c_2 i + c_3 j + c_4 k

with c1,c2,c3,c4c_1,c_2,c_3,c_4 as stated. (For instance the coefficient of 11 collects a1b1a_1b_1 from 1⋅11\cdot 1, −a2b2-a_2b_2 from i⋅i=−1i\cdot i = -1 times a2b2a_2b_2, and similarly −a3b3-a_3b_3, −a4b4-a_4b_4 from j2,k2j^2,k^2, giving c1=a1b1+a2b2+a3b3+a4b4c_1=a_1b_1+a_2b_2+a_3b_3+a_4b_4; the other coefficients are computed the same way using the table.) Then

N(α)N(β)=N(αβ)=c12+c22+c32+c42,N(\alpha)N(\beta) = N(\alpha\beta) = c_1^2+c_2^2+c_3^2+c_4^2,

which is precisely the claimed identity. ■\blacksquare

∎
CorollaryMultiplicative closure

If mm and nn are each sums of four squares, then so is mnmn.

Proof

Immediate from Euler's Four-Square Identity: write m=a12+a22+a32+a42m=a_1^2+a_2^2+a_3^2+a_4^2 and n=b12+b22+b32+b42n=b_1^2+b_2^2+b_3^2+b_4^2, then mn=c12+c22+c32+c42mn = c_1^2+c_2^2+c_3^2+c_4^2 for the explicit integers c1,…,c4c_1,\dots,c_4 (integer because each cic_i is an integer linear combination of integer ai,bja_i,b_j). ■\blacksquare

∎

By this corollary and unique factorization, it suffices to prove the theorem when nn is 00, 11, 22, or an odd prime pp: every positive integer n>1n>1 factors as a product of primes, each of which is a sum of four squares (once we handle p=2p=2 and odd primes), so nn itself is a sum of four squares by repeated application of the corollary. We record the trivial cases and then attack odd primes.

LemmaBase cases

0=02+02+02+020 = 0^2+0^2+0^2+0^2, 1=12+02+02+02\quad 1 = 1^2+0^2+0^2+0^2, 2=12+12+02+02\quad 2 = 1^2+1^2+0^2+0^2.

Proof

Immediate by inspection. ■\blacksquare

∎

Now fix an odd prime pp. The heart of the proof is:

LemmaSome small multiple of pp is a sum of four squares

Let pp be an odd prime. There exists an integer mm with 1≤m<p1\le m < p such that mpmp is a sum of four squares:

mp=a2+b2+c2+12for some integers a,b, and where the fourth square may be taken to be d2 with d∈{0,1} built in below.mp = a^2+b^2+c^2+1^2 \quad\text{for some integers } a,b, \text{ and where the fourth square may be taken to be } d^2 \text{ with } d\in\{0,1\}\text{ built in below.}

More precisely: there exist integers a,ba,b with

a2+b2+12+02≡0(modp),0≤a,b≤p−12,a^2+b^2+1^2+0^2 \equiv 0 \pmod p, \qquad 0\le a,b\le \tfrac{p-1}{2},

so that mp=a2+b2+1+0mp = a^2+b^2+1+0 for some positive integer m<pm<p.

Proof

This is a pigeonhole argument (technique: counting residues, due essentially to Lagrange/Euler and streamlined by later authors such as Ireland–Rosen). Consider the two sets

S={ a2  p:0≤a≤p−12},T={ −1−b2  p:0≤b≤p−12}.S = \left\{\, a^2 \bmod p : 0 \le a \le \tfrac{p-1}{2} \right\}, \qquad T = \left\{\, -1-b^2 \bmod p : 0 \le b \le \tfrac{p-1}{2} \right\}.

Each set has exactly p+12\frac{p+1}{2} elements. Indeed, for 0≤a1<a2≤p−120\le a_1 < a_2 \le \frac{p-1}{2}, if a12≡a22(modp)a_1^2\equiv a_2^2 \pmod p then p∣(a2−a1)(a2+a1)p \mid (a_2-a_1)(a_2+a_1); since 0<a2+a1≤p−1<p0 < a_2+a_1 \le p-1 < p, pp cannot divide a2+a1a_2+a_1, so p∣(a2−a1)p \mid (a_2-a_1), forcing a1=a2a_1=a_2 because 0≤a2−a1<p0\le a_2-a_1 < p. Hence the map a↦a2  pa \mapsto a^2 \bmod p is injective on {0,1,…,p−12}\{0,1,\dots,\frac{p-1}{2}\}, giving ∣S∣=p−12+1=p+12|S| = \frac{p-1}{2}+1 = \frac{p+1}{2} distinct residues. The identical injectivity argument applied to b↦−1−b2  pb\mapsto -1-b^2 \bmod p (composing an injective squaring map with the bijection x↦−1−x  px \mapsto -1-x \bmod p) shows ∣T∣=p+12|T| = \frac{p+1}{2} as well. Both SS and TT are subsets of the set of residues Z/pZ\mathbb{Z}/p\mathbb{Z}, which has exactly pp elements. Since

∣S∣+∣T∣=p+12+p+12=p+1>p,|S| + |T| = \frac{p+1}{2}+\frac{p+1}{2} = p+1 > p,

the pigeonhole principle forces S∩T≠∅S \cap T \neq \emptyset. So there exist a,ba,b with 0≤a,b≤p−120\le a,b\le \frac{p-1}{2} such that

a2≡−1−b2(modp),i.e.a2+b2+1≡0(modp).a^2 \equiv -1-b^2 \pmod p, \quad\text{i.e.}\quad a^2+b^2+1 \equiv 0 \pmod p.

Thus a2+b2+12+02=mpa^2+b^2+1^2+0^2 = mp for some non-negative integer mm. We bound mm: since 0≤a,b≤p−120\le a,b \le \frac{p-1}{2},

a2+b2+1≤2(p−12)2+1=(p−1)22+1<p22+1≤p2a^2+b^2+1 \le 2\left(\frac{p-1}{2}\right)^2+1 = \frac{(p-1)^2}{2}+1 < \frac{p^2}{2}+1 \le p^2

for p≥2p\ge 2 (in fact (p−1)22+1<p2\frac{(p-1)^2}{2}+1 < p^2 certainly holds for p≥2p \ge 2). Hence

mp=a2+b2+1<p2  ⟹  m<p.mp = a^2+b^2+1 < p^2 \implies m < p.

Also m≥1m\ge 1 since a2+b2+1≥1>0a^2+b^2+1 \ge 1 > 0 and mpmp must be a positive multiple of pp (it cannot be 00 since a2+b2+1≥1a^2+b^2+1\ge1). Thus 1≤m<p1 \le m < p, as claimed. ■\blacksquare

∎

The previous lemma shows some multiple mpmp with 1≤m<p1\le m<p is representable as a2+b2+c2+d2a^2+b^2+c^2+d^2 (here with c=1,d=0c=1,d=0, but we will need the fully general descent below, so we restate the induction in terms of an arbitrary representable multiple). We now perform the descent.

LemmaDescent step

Let pp be an odd prime, and suppose mm is the smallest positive integer such that mpmp is a sum of four squares. Then m=1m=1.

Proof

This is Lagrange's infinite descent (technique: minimal counterexample / descent, sharpened using Euler's identity — the modern streamlined form is due to later expositors, e.g. as in Ireland–Rosen). By Lemma: Some small multiple of pp is a sum of four squares, the set of positive integers mm such that mpmp is a sum of four squares is non-empty and contains an element less than pp; by well-ordering it has a least element mm, and 1≤m<p1\le m < p (in particular m≥1m\ge 1 exists and m≤m\le the bound already proved, so a minimum with 1≤m<p1\le m<p exists). Suppose, for contradiction, that m>1m>1. Write

mp=a2+b2+c2+d2mp = a^2+b^2+c^2+d^2

for some integers a,b,c,da,b,c,d.

Step 1: mm is odd. Suppose mm is even. Then a2+b2+c2+d2≡0(mod2)a^2+b^2+c^2+d^2 \equiv 0 \pmod 2. Among any four integers whose squares sum to an even number, we can always pair a,b,c,da,b,c,d into two pairs of equal parity: reduce mod 2, each of a,b,c,da,b,c,d is 00 or 11, and a2+b2+c2+d2a^2+b^2+c^2+d^2 even means the number of odd terms among a,b,c,da,b,c,d is even (0, 2, or 4). In every such case, we may reorder a,b,c,da,b,c,d so that a≡b(mod2)a\equiv b\pmod 2 and c≡d(mod2)c\equiv d \pmod 2 (if 00 or 44 are odd, any pairing works since all agree in a block of parity; if exactly 22 are odd, pair the two odd ones together and the two even ones together). Then a+ba+b, a−ba-b, c+dc+d, c−dc-d are all even, so

(a+b2)2+(a−b2)2+(c+d2)2+(c−d2)2=2(a2+b2)+2(c2+d2)4=a2+b2+c2+d22=m2p.\left(\frac{a+b}{2}\right)^2+\left(\frac{a-b}{2}\right)^2+\left(\frac{c+d}{2}\right)^2+\left(\frac{c-d}{2}\right)^2 = \frac{2(a^2+b^2)+2(c^2+d^2)}{4} = \frac{a^2+b^2+c^2+d^2}{2} = \frac{m}{2}p.

This exhibits m2p\frac{m}{2}p as a sum of four squares (of integers, since the four halved quantities are integers), and m2<m\frac{m}{2} < m is a smaller positive integer with m2p\frac{m}{2}p representable — contradicting minimality of mm. Hence mm must be odd.

Step 2: reduce a,b,c,da,b,c,d modulo mm. Since mm is odd and, by hypothesis, m>1m>1, we have m≥3m \ge 3. Choose integers a1,b1,c1,d1a_1,b_1,c_1,d_1 with

a1≡a, b1≡b, c1≡c, d1≡d(modm),−m2<a1,b1,c1,d1<m2a_1 \equiv a,\ b_1\equiv b,\ c_1 \equiv c,\ d_1\equiv d \pmod m, \qquad -\frac{m}{2} < a_1,b_1,c_1,d_1 < \frac{m}{2}

(possible since mm is odd: every residue class mod mm has a unique representative strictly between −m/2-m/2 and m/2m/2). Then

a12+b12+c12+d12≡a2+b2+c2+d2≡0(modm),a_1^2+b_1^2+c_1^2+d_1^2 \equiv a^2+b^2+c^2+d^2 \equiv 0 \pmod m,

so a12+b12+c12+d12=mna_1^2+b_1^2+c_1^2+d_1^2 = mn for some non-negative integer nn. Moreover, since each of a1,b1,c1,d1a_1,b_1,c_1,d_1 satisfies ∣a1∣<m/2|a_1| < m/2 etc.,

a12+b12+c12+d12<4(m2)2=m2,a_1^2+b_1^2+c_1^2+d_1^2 < 4\left(\frac{m}{2}\right)^2 = m^2,

so n<mn < m.

Step 3: rule out n=0n=0. If n=0n=0, then a1=b1=c1=d1=0a_1=b_1=c_1=d_1=0, meaning m∣am \mid a, m∣bm\mid b, m∣cm\mid c, m∣dm\mid d. Then m2∣a2+b2+c2+d2=mpm^2 \mid a^2+b^2+c^2+d^2 = mp, so m∣pm \mid p. Since 1<m<p1 < m < p and pp is prime, this is impossible (the only positive divisors of pp are 11 and pp). Hence n≠0n \neq 0, so n≥1n \ge 1.

Step 4: apply Euler's identity to (mp)(mn)=m2pn(mp)(mn) = m^2 pn. We have two representations

mp=a2+b2+c2+d2,mn=a12+b12+c12+d12,mp = a^2+b^2+c^2+d^2, \qquad mn = a_1^2+b_1^2+c_1^2+d_1^2,

so by Euler's Four-Square Identity,

m2pn=(mp)(mn)=e12+e22+e32+e42m^2pn = (mp)(mn) = e_1^2+e_2^2+e_3^2+e_4^2

where, using the identity with (a1,a2,a3,a4)→(a,b,c,d)(a_1,a_2,a_3,a_4) \to (a,b,c,d) and (b1,b2,b3,b4)→(a1,b1,c1,d1)(b_1,b_2,b_3,b_4)\to(a_1,b_1,c_1,d_1),

e1=aa1+bb1+cc1+dd1,e2=ab1−ba1+cd1−dc1,e3=ac1−bd1−ca1+db1,e4=ad1+bc1−cb1−da1.\begin{aligned} e_1 &= aa_1+bb_1+cc_1+dd_1,\\ e_2 &= ab_1-ba_1+cd_1-dc_1,\\ e_3 &= ac_1-bd_1-ca_1+db_1,\\ e_4 &= ad_1+bc_1-cb_1-da_1. \end{aligned}

Step 5: show each eie_i is divisible by mm, so that ei/me_i/m is an integer. Since a1≡a,b1≡b,c1≡c,d1≡d(modm)a_1\equiv a, b_1 \equiv b, c_1\equiv c, d_1\equiv d \pmod m, we compute modulo mm:

e1=aa1+bb1+cc1+dd1≡a2+b2+c2+d2=mp≡0(modm),e_1 = aa_1+bb_1+cc_1+dd_1 \equiv a^2+b^2+c^2+d^2 = mp \equiv 0 \pmod m,
e2=ab1−ba1+cd1−dc1≡ab−ba+cd−dc=0(modm),e_2 = ab_1-ba_1+cd_1-dc_1 \equiv ab-ba+cd-dc = 0 \pmod m,
e3=ac1−bd1−ca1+db1≡ac−bd−ca+db=0(modm),e_3 = ac_1-bd_1-ca_1+db_1 \equiv ac-bd-ca+db = 0 \pmod m,
e4=ad1+bc1−cb1−da1≡ad+bc−cb−da=0(modm).e_4 = ad_1+bc_1-cb_1-da_1 \equiv ad+bc-cb-da = 0 \pmod m.

So m∣e1,e2,e3,e4m \mid e_1,e_2,e_3,e_4, and we may set fi=ei/m∈Zf_i = e_i/m \in \mathbb{Z} for i=1,2,3,4i=1,2,3,4. Dividing the identity m2pn=e12+e22+e32+e42m^2pn = e_1^2+e_2^2+e_3^2+e_4^2 by m2m^2 gives

pn=f12+f22+f32+f42.pn = f_1^2+f_2^2+f_3^2+f_4^2.

Step 6: contradiction. This exhibits pnpn as a sum of four squares, with 1≤n<m1 \le n < m (we showed n≥1n\ge1 in Step 3 and n<mn<m in Step 2). This contradicts the minimality of mm as the smallest positive integer with mpmp a sum of four squares. Therefore the assumption m>1m>1 is false, and m=1m=1. ■\blacksquare

∎
Proof

(Proof of Lagrange's Four-Square Theorem.) We assemble the pieces (technique: multiplicative reduction to primes, then descent on primes — this is the full, non-circular proof). Base cases. By Lemma: Base cases, 0,1,20,1,2 are each sums of four squares. Odd primes. Let pp be an odd prime. By Lemma: Some small multiple of pp is a sum of four squares, the set {m≥1:mp is a sum of four squares}\{m \ge 1 : mp \text{ is a sum of four squares}\} is non-empty. By Lemma: Descent step, its least element is 11, i.e., pp itself is a sum of four squares. General nn. Every integer n≥2n\ge 2 factors as a product of primes,

n=q1q2⋯qr,n = q_1 q_2 \cdots q_r,

where each qi∈{2}∪{odd primes}q_i \in \{2\} \cup \{\text{odd primes}\} (with repetition allowed). Each qiq_i is a sum of four squares, by the base case qi=2q_i=2 or by the odd-prime case just proved. By repeated application of Corollary: Multiplicative closure (an induction on rr: the product of the first kk factors is a sum of four squares, and multiplying by the (k+1)(k+1)-st factor — itself a sum of four squares — preserves this by the corollary), n=q1⋯qrn = q_1\cdots q_r is a sum of four squares. Together with n=0,1n=0,1 from the base case, every non-negative integer nn is a sum of four squares. ■\blacksquare

∎

Worked examples

Example 1: the prime p=23p=23. We illustrate the pigeonhole lemma and descent concretely, choosing an asymmetric case where the naive small representation is not immediately obvious. Step A (pigeonhole). We need a,ba,b with 0≤a,b≤110 \le a,b \le 11 and a2+b2+1≡0(mod23)a^2+b^2+1 \equiv 0 \pmod{23}. Compute squares mod 23 for a=0,…,11a=0,\dots,11:

0,1,4,9,16,2,13,3,18,12,8,60,1,4,9,16,2,13,3,18,12,8,6

(e.g. 62=36≡136^2=36\equiv13, 72=49≡37^2=49\equiv3, 82=64≡188^2=64\equiv18, 92=81≡129^2=81\equiv 12, 102=100≡810^2=100\equiv 8, 112=121≡611^2=121\equiv 6). We want a2≡−1−b2(mod23)a^2 \equiv -1-b^2 \pmod {23}, i.e. a2+b2≡22(mod23)a^2+b^2 \equiv 22 \pmod {23}. Scanning pairs: with b=8b=8, b2≡18b^2\equiv 18, need a2≡22−18=4(mod23)a^2 \equiv 22-18=4 \pmod{23}, so a=2a=2 works (22=42^2=4). Check: a2+b2+1=4+64+1=69=3⋅23a^2+b^2+1 = 4+64+1=69 = 3\cdot 23. So m=3m=3. Step B (descent from m=3m=3 to m=1m=1). We have 3⋅23=22+82+12+023\cdot 23 = 2^2+8^2+1^2+0^2 with (a,b,c,d)=(2,8,1,0)(a,b,c,d)=(2,8,1,0). Reduce each coordinate mod 33 to the representative in (−3/2,3/2)=(−1.5,1.5)(-3/2,3/2) = (-1.5,1.5), i.e. residues in {−1,0,1}\{-1,0,1\}:

2≡−1,8≡−1,1≡1,0≡0(mod3),2 \equiv -1, \quad 8 \equiv -1, \quad 1\equiv 1, \quad 0 \equiv 0 \pmod 3,

so (a1,b1,c1,d1)=(−1,−1,1,0)(a_1,b_1,c_1,d_1) = (-1,-1,1,0). Then

a12+b12+c12+d12=1+1+1+0=3=3n  ⟹  n=1.a_1^2+b_1^2+c_1^2+d_1^2 = 1+1+1+0 = 3 = 3n \implies n=1.

Good — n=1<m=3n=1<m=3 as guaranteed, and n≠0n\ne 0 as the proof requires. Now apply Euler's identity to (mp)(mn)=(69)(3)=207=9⋅23(mp)(mn) = (69)(3) = 207 = 9\cdot 23:

e1=aa1+bb1+cc1+dd1=(2)(−1)+(8)(−1)+(1)(1)+(0)(0)=−2−8+1+0=−9,e2=ab1−ba1+cd1−dc1=(2)(−1)−(8)(−1)+(1)(0)−(0)(1)=−2+8+0−0=6,e3=ac1−bd1−ca1+db1=(2)(1)−(8)(0)−(1)(−1)+(0)(−1)=2−0+1+0=3,e4=ad1+bc1−cb1−da1=(2)(0)+(8)(1)−(1)(−1)−(0)(−1)=0+8+1−0=9.\begin{aligned} e_1 &= aa_1+bb_1+cc_1+dd_1 = (2)(-1)+(8)(-1)+(1)(1)+(0)(0) = -2-8+1+0=-9,\\ e_2 &= ab_1-ba_1+cd_1-dc_1 = (2)(-1)-(8)(-1)+(1)(0)-(0)(1) = -2+8+0-0 = 6,\\ e_3 &= ac_1-bd_1-ca_1+db_1 = (2)(1)-(8)(0)-(1)(-1)+(0)(-1) = 2-0+1+0 = 3,\\ e_4 &= ad_1+bc_1-cb_1-da_1 = (2)(0)+(8)(1)-(1)(-1)-(0)(-1) = 0+8+1-0 = 9. \end{aligned}

Check: e12+e22+e32+e42=81+36+9+81=207e_1^2+e_2^2+e_3^2+e_4^2 = 81+36+9+81 = 207. Indeed 207=9⋅23207 = 9\cdot 23. Each eie_i should be divisible by m=3m=3: −9,6,3,9-9,6,3,9 are all multiples of 33. Dividing by m=3m=3:

f1=−3, f2=2, f3=1, f4=3,f_1=-3,\ f_2=2,\ f_3=1,\ f_4=3,

and

f12+f22+f32+f42=9+4+1+9=23=p⋅n=23⋅1.f_1^2+f_2^2+f_3^2+f_4^2 = 9+4+1+9 = 23 = p\cdot n = 23\cdot 1.

So 23=(−3)2+22+12+32=9+4+1+9=2323 = (-3)^2+2^2+1^2+3^2 = 9+4+1+9=23. We have descended in one step directly to n=1n=1, giving an explicit four-square representation

23=32+22+12+32.23 = 3^2+2^2+1^2+3^2.

Example 2: an arbitrary composite number, n=2023n=2023. Factor 2023=7×1722023 = 7\times 17^2. We need four-square representations of 77 and of 1717, then combine via Euler's identity. Four squares for 77: 7=22+12+12+127 = 2^2+1^2+1^2+1^2 (4+1+1+1=74+1+1+1=7). Four squares for 1717: 17=42+12+02+0217 = 4^2+1^2+0^2+0^2 (16+1=1716+1=17). Combine 1717 with itself using Euler's identity to get 172=28917^2=289: take (a1,a2,a3,a4)=(b1,b2,b3,b4)=(4,1,0,0)(a_1,a_2,a_3,a_4)=(b_1,b_2,b_3,b_4)=(4,1,0,0). Then

c1=4⋅4+1⋅1+0+0=17,c2=4⋅1−1⋅4+0−0=0,c3=4⋅0−1⋅0−0⋅4+0⋅1=0,c4=4⋅0+1⋅0−0⋅1−0⋅4=0.\begin{aligned} c_1 &= 4\cdot4+1\cdot1+0+0 = 17,\\ c_2 &= 4\cdot1-1\cdot4+0-0=0,\\ c_3 &= 4\cdot0-1\cdot0-0\cdot4+0\cdot1=0,\\ c_4 &= 4\cdot0+1\cdot0-0\cdot1-0\cdot4=0. \end{aligned}

So 289=172+02+02+02289 = 17^2+0^2+0^2+0^2 — unsurprising since 17217^2 is trivially a perfect square, but it confirms the identity mechanics. Combine 77 and 289289: take (a1,a2,a3,a4)=(2,1,1,1)(a_1,a_2,a_3,a_4) = (2,1,1,1) (representation of 77) and (b1,b2,b3,b4)=(17,0,0,0)(b_1,b_2,b_3,b_4)=(17,0,0,0) (representation of 289289). Then

c1=a1b1+a2b2+a3b3+a4b4=2⋅17+1⋅0+1⋅0+1⋅0=34,c2=a1b2−a2b1+a3b4−a4b3=0−17+0−0=−17,c3=a1b3−a2b4−a3b1+a4b2=0−0−17+0=−17,c4=a1b4+a2b3−a3b2−a4b1=0+0−0−17=−17.\begin{aligned} c_1 &= a_1b_1+a_2b_2+a_3b_3+a_4b_4 = 2\cdot17+1\cdot0+1\cdot0+1\cdot0 = 34,\\ c_2 &= a_1b_2-a_2b_1+a_3b_4-a_4b_3 = 0 - 17+0-0=-17,\\ c_3 &= a_1b_3-a_2b_4-a_3b_1+a_4b_2 = 0-0-17+0=-17,\\ c_4 &= a_1b_4+a_2b_3-a_3b_2-a_4b_1 = 0+0-0-17=-17. \end{aligned}

Check: c12+c22+c32+c42=342+172+172+172=1156+289+289+289=2023c_1^2+c_2^2+c_3^2+c_4^2 = 34^2+17^2+17^2+17^2 = 1156+289+289+289 = 2023. And indeed 7×289=20237\times 289 = 2023. So

2023=342+172+172+172.2023 = 34^2+17^2+17^2+17^2.

As a sanity check by direct arithmetic: 342=115634^2=1156, and 3×172=3×289=8673\times 17^2 = 3\times289=867, and 1156+867=20231156+867=2023. Correct. (This differs from, but is equally valid alongside, the representation 2023=442+62+12+122023=44^2+6^2+1^2+1^2 mentioned in the overview — four-square representations need not be unique.)

CounterexampleWhy "four" cannot be weakened to "three"

Drop nothing about the integers involved, but weaken the hypothesis on the number of squares from four to three: consider the claim "every non-negative integer is a sum of three squares." This is false, and the standard counterexample is n=7n=7. Check every combination of three squares from {0,1,4}\{0,1,4\} (since 9>79>7, no square exceeding 44 can appear): the possible sums using a2,b2,c2∈{0,1,4}a^2,b^2,c^2\in\{0,1,4\} are

0,1,2,3,4,5,6,8,9,120,1,2,3,4,5,6,8,9,12

— explicitly: 0+0+0=00+0+0=0, 0+0+1=10+0+1=1, 0+1+1=20+1+1=2, 1+1+1=31+1+1=3, 0+0+4=40+0+4=4, 0+1+4=50+1+4=5, 1+1+4=61+1+4=6, 0+4+4=80+4+4=8, 1+4+4=91+4+4=9, 4+4+4=124+4+4=12. The number 77 never appears among these sums. Since any three squares summing to 77 would have to be built entirely from {0,1,4}\{0,1,4\} (as 9>79 > 7), and none of the ten combinations above equals 77, we conclude 77 is not a sum of three squares. This matches the general theorem of Legendre (proof beyond the scope of this article): nn is a sum of three squares if and only if nn is not of the form 4a(8k+7)4^a(8k+7) for non-negative integers a,ka,k. Here 7=40(8⋅0+7)7 = 4^0(8\cdot 0+7) is exactly of the excluded form. This shows the hypothesis "four squares" in Lagrange's theorem is not an artifact of a weak proof technique — it is numerically necessary: dropping to three squares genuinely breaks universality, with 77 as the smallest failure.

Scope, edge cases & common traps

SituationWhat happensWhy
n=0n=00=02+02+02+020=0^2+0^2+0^2+0^2trivial, included by convention
nn a perfect squaree.g. 16=42+02+02+0216=4^2+0^2+0^2+0^2zeros are allowed, so squares are trivially sums of four squares
Only three squares allowedfails for n=7,15,23,28,…n=7,15,23,28,\dots (Legendre's 4a(8k+7)4^a(8k+7))three squares miss an entire congruence family
Requiring a,b,c,d>0a,b,c,d>0 strictlyfails for small nn (e.g. n=1,2,3n=1,2,3 cannot be written with four positive squares)zero must be allowed as a summand in general
Uniqueness of representationfalse — e.g. 2023=342+172+172+172=442+62+12+122023 = 34^2+17^2+17^2+17^2 = 44^2+6^2+1^2+1^2the theorem asserts existence, not uniqueness; the number of representations is governed by Jacobi's four-square formula, a separate refinement
Negative integerstheorem does not applysquares are non-negative, so only n≥0n\ge0 can be represented

The mathematician behind it

Origins · 1 minJoseph-Louis Lagrange and how this was discovered
JL
Joseph-Louis Lagrange
1736–1813 · Ancien Régime and Napoleonic France, mathematical analysis and number theory

Lagrange was born in Turin and rose to become, alongside Euler, the dominant analytical mathematician of the eighteenth century. He worked at the Berlin Academy under Frederick the Great and later at the Paris institutions reorganized after the Revolution, contributing foundational work to mechanics (the Mécanique analytique), the calculus of variations, and algebra (early group-theoretic ideas behind the unsolvability of the quintic). In number theory, building on conjectures traceable to Diophantus, Bachet, and Fermat, Lagrange gave in 1770 the first complete proof that every positive integer is a sum of four integer squares, using a descent method sharpened by Euler's four-square identity — itself discovered by Euler while attempting the same problem without quite closing the descent argument.

History
- 1621: Bachet, editing Diophantus's Arithmetica, conjectures every positive integer is a sum of at most four squares.
- 1747–1750s: Euler discovers the four-square multiplicative identity while working on the problem, but cannot complete the descent for all primes.
- 1770: Lagrange publishes the first complete proof of the four-square theorem, combining Euler's identity with an infinite-descent argument.
- 1772: Euler gives a simplified proof using his identity more efficiently, following Lagrange's breakthrough.
- 1834 (and later, 1828–1834 in various forms): Jacobi proves an exact formula for the number of representations of nn as an ordered sum of four squares, refining Lagrange's qualitative existence theorem into a precise count.
Does the four-square theorem give a unique representation for each nn?

No. Existence, not uniqueness, is what Lagrange's theorem asserts. Many integers have several genuinely different representations as ordered or unordered sums of four squares — for instance 2023=342+172+172+172=442+62+12+122023 = 34^2+17^2+17^2+17^2 = 44^2+6^2+1^2+1^2, two distinct decompositions. The precise count of ordered representations (with signs and order counted) is given by Jacobi's formula, r4(n)=8∑d∣n,4∤ddr_4(n) = 8\sum_{d\mid n, 4\nmid d} d, which is a much finer statement than mere existence and requires the separate machinery of modular forms and theta functions to prove.

Why does the proof need Euler's identity at all — couldn't we just try to show every prime is representable directly and add representations coordinate-wise?

Sums of squares do not add coordinate-wise in general: if m=a2+b2+c2+d2m=a^2+b^2+c^2+d^2 and n=e2+f2+g2+h2n=e^2+f^2+g^2+h^2, there is no reason mnmn should equal (a+e)2+(b+f)2+⋯(a+e)^2+(b+f)^2+\cdots. What makes mnmn representable is a genuinely multiplicative structure — the quaternion norm being multiplicative — captured precisely by Euler's identity. Without it, one would have to prove representability for every integer directly, an intractable task; the identity is what allows reducing the infinite problem (all nn) down to a finite-flavored one (all primes).

Is the four-square theorem related to sums of two squares?

Yes, both are instances of the same phenomenon at different "orders": the norm form on the Gaussian integers Z[i]\mathbb{Z}[i] is multiplicative for two squares (as used in Sums of Two Squares), while the norm form on the Hamilton quaternions is multiplicative for four squares. Two squares have genuine obstructions (primes ≡3(mod4)\equiv3\pmod4 to an odd power block representability), while four squares have none — the extra "room" in four real dimensions removes every obstruction, essentially because the quaternions, unlike a hypothetical two-real-dimension-only analogue, supply enough multiplicative structure combined with the descent to reach every prime.

Why does the pigeonhole argument require 0≤a,b≤(p−1)/20\le a,b\le (p-1)/2 rather than all residues 0,…,p−10,\dots,p-1?

Restricting to a,b∈{0,1,…,p−12}a,b \in \{0,1,\dots,\frac{p-1}{2}\} is what makes the two sets S={a2  p}S=\{a^2 \bmod p\} and T={−1−b2  p}T=\{-1-b^2\bmod p\} each have exactly p+12\frac{p+1}{2} distinct elements (squaring is 2-to-1 on the full range 0,…,p−10,\dots,p-1, collapsing aa and p−ap-a to the same square, so restricting to half the range removes the redundancy). This exact count is what forces ∣S∣+∣T∣=p+1>p|S|+|T| = p+1 > p, guaranteeing overlap by pigeonhole. Using the full range would double-count and could make the pigeonhole bound too weak to guarantee an intersection.

Does the theorem hold for negative integers or non-integers?

No — by definition, a sum of squares of real numbers is always non-negative, so no negative integer can be a sum of four squares (or any number of squares). For non-integers such as rational numbers, a related but different theory exists (e.g., every non-negative rational is a sum of four rational squares, provable from the integer case by clearing denominators and using the identity), but the theorem as stated is specifically about non-negative integers decomposed using integer square roots.

How does this connect to Waring's problem?

Waring's problem asks, for each exponent k≥2k\ge2, for the minimal number g(k)g(k) such that every positive integer is a sum of at most g(k)g(k) kk-th powers. Lagrange's theorem is exactly the statement g(2)=4g(2)=4 (three is not enough, as the counterexample n=7n=7 shows, and four always suffices). For higher kk the analogous minimal number g(k)g(k) is far harder to determine and was only fully resolved for many exponents in the twentieth century using the Hardy–Littlewood circle method; the four-square theorem remains the cleanest, fully elementary base case of this entire research program.

Before this