[proofplan]
The necessity direction is a direct residue computation modulo $8$: squares modulo $8$ lie in $\{0, 1, 4\}$, so sums of three squares modulo $8$ cannot equal $7$; a division-by-$4$ argument then extends this obstruction to integers of the form $4^a(8b + 7)$. The sufficiency direction is deeper: following the classical Gauss-Dirichlet approach, one reduces to showing that every $n \not\equiv 0, 4, 7 \pmod 8$ is represented by the principal ternary form $x_1^2 + x_2^2 + x_3^2$. This uses two ingredients: a local-global principle reducing the representability problem to local solubility modulo every prime power, which in turn reduces (by Hensel's lemma) to modulo small prime powers; and the Minkowski-style lattice argument that shows every positive definite ternary form of class number one represents every integer locally representable by its genus. The square-free reduction $n = 4^a n_0$ shifts the problem to $n_0 \not\equiv 0, 4, 7 \pmod 8$, for which the local-global step applies cleanly.
[/proofplan]
[step:Necessity: rule out $n = 4^a(8b + 7)$]
We first show: if $n = 4^a(8b + 7)$ for non-negative integers $a, b$, then $n$ is not a sum of three squares.
**Residues of squares modulo $8$.** For $x \in \mathbb{Z}$, the residue of $x^2$ modulo $8$ depends only on $x \bmod 4$. Direct computation:
\begin{align*}
0^2 \equiv 0, \quad 1^2 \equiv 1, \quad 2^2 \equiv 4, \quad 3^2 \equiv 1, \quad 4^2 \equiv 0, \quad 5^2 \equiv 1, \quad 6^2 \equiv 4, \quad 7^2 \equiv 1 \pmod 8.
\end{align*}
Hence $\{x^2 \bmod 8 : x \in \mathbb{Z}\} = \{0, 1, 4\}$.
**Sums of three squares modulo $8$.** The sums $a + b + c$ with $a, b, c \in \{0, 1, 4\}$ take values in $\{0, 1, 2, 3, 4, 5, 6, 8, 9, 12\} \bmod 8 = \{0, 1, 2, 3, 4, 5, 6\}$. Indeed, the maximum (up to reducing modulo $8$) is $4 + 4 + 4 = 12 \equiv 4$, and inspection of all $3 \cdot 3 \cdot 3 = 27$ cases shows $7 \bmod 8$ never occurs. In particular, if $n \equiv 7 \pmod 8$, then $n$ is not a sum of three squares. This handles $a = 0$.
**Descent by factors of $4$.** Suppose $a \geq 1$ and $4n' = 4 \cdot 4^{a-1}(8b+7)$ is a sum of three squares: $4n' = x_1^2 + x_2^2 + x_3^2$. Reducing modulo $4$: $x_1^2 + x_2^2 + x_3^2 \equiv 0 \pmod 4$. Each $x_i^2 \in \{0, 1\} \pmod 4$, so the only way the sum vanishes is all $x_i^2 \equiv 0 \pmod 4$, i.e. all $x_i$ are even. Writing $x_i = 2 y_i$:
\begin{align*}
4 n' = 4 (y_1^2 + y_2^2 + y_3^2), \quad \text{so} \quad n' = y_1^2 + y_2^2 + y_3^2.
\end{align*}
Thus if $4n'$ is a sum of three squares, so is $n'$. Iterating this $a$ times, if $4^a(8b+7)$ is a sum of three squares, so is $8b + 7$. By the $a = 0$ case, this is impossible. Hence $n = 4^a(8b+7)$ is not a sum of three squares.
[guided]
We establish necessity in two stages: first the base case $a = 0$, which is a clean modular obstruction, then a descent argument that extends the obstruction to all $a \geq 0$.
**Stage 1: squares modulo 8.** The question "which residues modulo $8$ are squares?" has an explicit answer via the Chinese remainder-style enumeration: $x \bmod 4$ determines $x^2 \bmod 8$, and the four residue classes $0, 1, 2, 3$ produce $x^2 \equiv 0, 1, 4, 1$. So squares modulo $8$ land in $\{0, 1, 4\}$.
Now, which residues modulo $8$ are sums of three such squares? The set of sums $\{a + b + c : a, b, c \in \{0, 1, 4\}\}$, reduced modulo $8$, can be listed:
\begin{align*}
0+0+0 &= 0, & 0+0+1 &= 1, & 0+0+4 &= 4, \\
0+1+1 &= 2, & 0+1+4 &= 5, & 0+4+4 &= 8 \equiv 0, \\
1+1+1 &= 3, & 1+1+4 &= 6, & 1+4+4 &= 9 \equiv 1, \\
4+4+4 &= 12 &\equiv 4. & & &
\end{align*}
The set of achievable residues is $\{0, 1, 2, 3, 4, 5, 6\}$ — every residue **except** $7$. Hence no integer $n \equiv 7 \pmod 8$ is a sum of three squares.
**Stage 2: descent from $4n$ to $n$.** The key observation is: if $4n' = x_1^2 + x_2^2 + x_3^2$, then all $x_i$ are even. Why? Modulo $4$, we have $x_i^2 \in \{0, 1\}$ (squares modulo $4$ are $0$ or $1$, from $0^2, 1^2, 2^2, 3^2 \equiv 0, 1, 0, 1$). For $\sum x_i^2 \equiv 0 \pmod 4$, we need the $x_i^2 \bmod 4$ to sum to $0$ modulo $4$. Possible combinations: $(0,0,0)$, or $(1,1,1,1) \ldots$ but we have only three summands, so $(1, 1, 1) \equiv 3 \pmod 4 \neq 0$; $(1, 1, 0) \equiv 2 \pmod 4 \neq 0$; $(1, 0, 0) \equiv 1 \pmod 4 \neq 0$. Only $(0, 0, 0)$ works, meaning all $x_i$ are even.
Writing $x_i = 2 y_i$ and dividing by $4$: $n' = y_1^2 + y_2^2 + y_3^2$. So representability of $4 n'$ implies representability of $n'$. By induction, representability of $4^a m$ implies representability of $m$; contrapositively, if $m = 8b + 7$ is not representable (by Stage 1), then $4^a m$ is not representable.
[/guided]
[/step]
[step:Sufficiency setup: reduce to the square-free-like case]
For the converse, suppose $n \geq 1$ is not of the form $4^a(8b + 7)$. Write $n = 4^a n_0$ where $4 \nmid n_0$. Then $n_0 \in \{1, 2, 3, 5, 6, 7\} \pmod 8 \setminus \{7\}$ — that is, $n_0 \not\equiv 0, 4, 7 \pmod 8$. (If $n_0 \equiv 7 \pmod 8$ then $n = 4^a(8b+7)$ for some $b$, contradicting the hypothesis.)
**Observation:** if $n_0$ is a sum of three squares, $n_0 = u_1^2 + u_2^2 + u_3^2$, then $n = 4^a n_0 = (2^a u_1)^2 + (2^a u_2)^2 + (2^a u_3)^2$ is too. So it suffices to prove: every $n_0 \geq 1$ with $n_0 \not\equiv 0, 4, 7 \pmod 8$ is a sum of three squares.
In the remaining steps, we fix such an $n_0$ and denote it simply by $n$. We have $n \not\equiv 0, 4, 7 \pmod 8$.
[/step]
[step:Local solubility: verify $x^2 + y^2 + z^2 \equiv n$ is soluble modulo every prime power]
We apply the theorem of Minkowski-Hasse for ternary quadratic forms: a positive integer $n$ is represented by the form $Q(x, y, z) = x^2 + y^2 + z^2$ over $\mathbb{Z}$ if and only if it is represented over $\mathbb{R}$ (which is automatic for $n > 0$) and over $\mathbb{Z}_p$ for every prime $p$, subject to the further condition that the form and its restriction lie in the same genus (which for $Q$ is the case, since the genus of $Q$ contains only $Q$ — the class number of the principal ternary form is one for these discriminants).
**Local representability: $p$ odd.** For any odd prime $p$ and any $n$, the equation $x^2 + y^2 + z^2 \equiv n \pmod{p^k}$ is soluble. Indeed, modulo $p$, the $p$-variable equation $x^2 + y^2 \equiv n - z^2$ is soluble: the sets $A_z = \{x^2 + y^2 \bmod p\}$ and $\{n - z^2 \bmod p\}$ both have size $\geq (p+1)/2$ as $z$ ranges over $\mathbb{Z}/p\mathbb{Z}$, with $|A| \geq p$ by a standard pigeonhole (the set of sums of two squares modulo $p$ is all of $\mathbb{F}_p$). Once a solution exists modulo $p$ with not all of $x, y, z \equiv 0 \pmod p$, [Hensel Lifting for Squares](/theorems/1739) combined with the general Hensel-type argument for ternary forms lifts this to a solution modulo every $p^k$.
**Local representability: $p = 2$.** The hypothesis $n \not\equiv 0, 4, 7 \pmod 8$ is exactly the condition for solubility of $x^2 + y^2 + z^2 \equiv n \pmod 8$. By the residue computation in Step 1, the attainable residues modulo $8$ for sums of three squares are $\{0, 1, 2, 3, 4, 5, 6\}$; our $n$ lies in $\{1, 2, 3, 5, 6\} \pmod 8$, which is contained in this set. The local refinement lifts a solution modulo $8$ to one modulo $2^k$ for all $k \geq 1$; this uses [Hensel Lifting for Squares](/theorems/1739) and a multivariable extension to ternary forms (the non-degeneracy of $\nabla Q = 2(x, y, z)$ provides the invertible derivative needed for Hensel lifting, provided the solution modulo $2^3$ is primitive — i.e. not all of $x, y, z$ divisible by $2$, which follows from $n \not\equiv 0 \pmod 4$ when $n$ is considered after taking out the factor $4^a$).
[/step]
[step:Pass from local to global using the class number one property of the principal ternary genus]
Define the principal ternary form
\begin{align*}
Q : \mathbb{Z}^3 &\to \mathbb{Z}, \\
(x_1, x_2, x_3) &\mapsto x_1^2 + x_2^2 + x_3^2.
\end{align*}
This is a positive definite ternary quadratic form of discriminant $-4$ (in the convention $\operatorname{disc}(Q) = -\det(2A)$ where $A = I_3$ is the Gram matrix).
The genus of $Q$ is the set of equivalence classes of positive definite ternary forms that are $\mathbb{Z}_p$-equivalent to $Q$ for every prime $p$. By the classical theory of ternary forms (Gauss's Disquisitiones Arithmeticae, Article 291), the genus of $Q$ contains only the single class of $Q$ itself — equivalently, the class number of the genus of $Q$ equals $1$. Concretely: every positive definite ternary form with the same local invariants as $Q$ is $\mathrm{SL}_3(\mathbb{Z})$-equivalent to $Q$.
By the local-global principle for ternary quadratic forms (Hasse's theorem, specialised to ternary forms of class number one), an integer $n$ is represented by **some** form in the genus of $Q$ if and only if it is represented over $\mathbb{R}$ and over every $\mathbb{Z}_p$. Since the genus of $Q$ consists only of $Q$, this equivalence sharpens to: $n$ is represented by $Q$ itself if and only if $n > 0$ and $n$ is represented over every $\mathbb{Z}_p$.
Both conditions hold by the previous step: $n > 0$ by hypothesis, and local solubility at every prime was verified. Therefore there exist $x_1, x_2, x_3 \in \mathbb{Z}$ with $x_1^2 + x_2^2 + x_3^2 = n$.
[guided]
This is the deepest step, and we unpack its ingredients.
**The Hasse principle for ternary forms** asserts that for a positive definite ternary quadratic form $F$, representation of a positive integer $n$ by some form in the **genus** of $F$ is equivalent to representation over $\mathbb{R}$ and over each $\mathbb{Z}_p$. Two forms are in the same genus when they are equivalent locally at every place — over $\mathbb{R}$ and over each $\mathbb{Z}_p$.
For ternary forms, the genus of $F$ may contain several equivalence classes. The principle then says only that *some* class in the genus represents $n$ globally; individual classes may miss some integers.
**The class number one phenomenon.** Fortunately, for the principal form $Q = x^2 + y^2 + z^2$ (discriminant $-4$), the genus contains a single $\mathrm{SL}_3(\mathbb{Z})$-equivalence class — namely, $Q$ itself. This is a consequence of Gauss's classification of ternary forms: the genus of $Q$, being that of the identity matrix, is easily shown to consist only of $Q$ up to $\mathrm{SL}_3(\mathbb{Z})$-equivalence. Hence "represented by some form in the genus" collapses to "represented by $Q$".
**Verifying local solubility.**
- **At $\mathbb{R}$:** $n > 0$ is represented by any positive definite form (and in particular by $Q$, since we can solve $x_1^2 + x_2^2 + x_3^2 = n$ with real $x_i$, for instance $x_1 = \sqrt{n}$, $x_2 = x_3 = 0$).
- **At odd primes $p$:** A counting argument shows the map $(x, y, z) \mapsto x^2 + y^2 + z^2$ is surjective modulo $p$; Hensel lifting (applicable because $\nabla Q = (2x, 2y, 2z)$ is non-vanishing wherever one coordinate is a unit modulo $p$) upgrades this to $\mathbb{Z}_p$-solubility.
- **At $p = 2$:** This is the delicate case. The hypothesis $n \not\equiv 0, 4, 7 \pmod 8$ is exactly the condition for representability modulo $8$ (from Step 1's residue analysis, the attainable residues are $\{0, 1, 2, 3, 4, 5, 6\} \bmod 8$; the missing residue $7 \bmod 8$ is the obstruction we ruled out). Once representability modulo $8$ is established with a primitive triple (not all coordinates even), a 2-adic Hensel argument lifts to solubility over $\mathbb{Z}_2$.
**Combining.** Local solubility everywhere plus class-number-one gives representability by $Q$. This is the global representation $n = x_1^2 + x_2^2 + x_3^2$ we sought.
**Remark on the deeper structure.** The Hasse principle for ternary forms is not a cheap result: it relies on quadratic reciprocity (via the Hilbert symbol) and the genus theory of Gauss. The class number one statement for the principal genus is the other essential ingredient. Together they express that the principal form $x^2 + y^2 + z^2$ — the simplest positive definite ternary form — represents every integer that the local obstructions (modulo $8$) permit, and no more.
[/guided]
[/step]
[step:Conclude]
Combining necessity and sufficiency: an integer $n \geq 1$ is a sum of three integer squares if and only if $n$ is not of the form $4^a(8b + 7)$ for non-negative integers $a, b$. The necessity was established in Step 1 by modular arithmetic modulo $8$ and a descent by factors of $4$. The sufficiency was established by reducing to the case $n \not\equiv 0, 4, 7 \pmod 8$, establishing local solubility at every prime and at $\mathbb{R}$, and invoking the Hasse principle for the principal ternary form $x^2 + y^2 + z^2$, whose genus contains only itself. This completes the proof of Legendre's Three-Square Theorem.
[/step]