Legendre's three-square theorem asserts that a natural number n can be expressed as the sum of three squares of integers—that is, n = x^2 + y^2 + z^2 for some integers x, y, z—if and only if n is not of the form $4^k(8m + 7) where k and m are non-negative integers.[1][](http://simonrs.com/eulercircle/number theory/jon-ternaryqf.pdf)Named after the French mathematician Adrien-Marie Legendre, who stated the theorem with a partial proof in 1798, completed by Carl Friedrich Gauss in 1801 in his Disquisitiones Arithmeticae, the theorem resolves a long-standing question in number theory dating back to ancient inquiries into representing numbers as sums of squares.[2] Legendre's result built upon earlier work by Fermat and Euler on sums of two and four squares, completing the picture for three squares by identifying the precise obstruction via the forbidden form modulo 8 and powers of 4.[1]The theorem's proof involves two directions: the necessity follows from observing that squares modulo 8 are 0, 1, or 4, so their sum cannot be 7 modulo 8, and extending this via powers of 4; the sufficiency requires more advanced techniques, such as Dirichlet's 1850 analytic proof using primes in arithmetic progressions or modern geometric approaches via Minkowski's convex body theorem.[](http://simonrs.com/eulercircle/number theory/jon-ternaryqf.pdf)This result has profound implications in additive number theory, influencing studies of quadratic forms, the distribution of sums of squares, and generalizations to sums of triangular numbers or other figurate numbers, while also connecting to broader themes in the theory of representations by indefinite ternaryquadratic forms.[1][3]
In number theory, a positive integer n is representable as a sum of k squares if there exist integers x_1, x_2, \dots, x_k such that n = x_1^2 + x_2^2 + \dots + x_k^2.[4] This concept forms a cornerstone of the study of Diophantine equations, which seek integer solutions to polynomial equations.[5]The historical motivation for investigating sums of squares traces back to ancient problems in Diophantine analysis, notably the equation x^2 + y^2 = z^2, whose positive integer solutions are known as Pythagorean triples.[6] These triples were documented by Babylonian mathematicians around 1800 BC on clay tablets like Plimpton 322, reflecting early interest in geometric and arithmetic properties of right triangles.[6] Later, Diophantus of Alexandria (circa 200–284 AD) explored representations involving squares in his treatise Arithmetica, laying foundational work for algebraic number theory by seeking rational and integer solutions to such equations.[7]Sums of squares are studied for their deep connections to quadratic forms—homogeneous polynomials of degree two—and their role in understanding arithmetic structures like progressions and modular properties.[8] These investigations reveal patterns in integer representations and influence broader areas, including class number problems and the distribution of primes.[8] For example, the number 1 can be written as $1^2, 2 as $1^2 + 1^2, and 5 as $1^2 + 2^2, illustrating how small integers often admit simple decompositions.[4] The specific case of three squares, which determines representability for many integers, is addressed in detail by Legendre's theorem.[4]
Modular Arithmetic Prerequisites
In number theory, quadratic residues play a crucial role in the study of Diophantine equations, particularly those involving sums of squares, as they determine whether an integer can be expressed in specific quadratic forms modulo a given integer, thereby providing necessary conditions for solvability over the integers.[9] The concept originates from the solvability of quadratic congruences x^2 \equiv a \pmod{m}, where a is a quadratic residue modulo m if such an x exists, enabling local-global principles like Hasse's theorem to bridge modular solutions to global ones in quadratic Diophantine problems.[10]A fundamental example arises modulo 8, where the possible values of squares are limited. For any integer x, x^2 \equiv 0, [1](/page/1), or $4 \pmod{8}.[11] To see this, consider the parity of x: if x is even, write x = 2k; then x^2 = 4k^2, and if k is even, k = 2l so x^2 = 16l^2 \equiv 0 \pmod{8}; if k is odd, k = 2l + 1 so x^2 = 4(4l^2 + 4l + 1) = 16l^2 + 16l + 4 \equiv 4 \pmod{8}. If x is odd, x = 2k + 1 so x^2 = 4k^2 + 4k + 1 \equiv [1](/page/1) \pmod{8}.[12] Thus, the quadratic residues modulo 8 are precisely 0, 1, and 4, excluding 2, 3, 5, 6, and 7.[11]These residues constrain sums of squares modulo 8. The possible sums of three squares modulo 8 are the combinations of {0, 1, 4}:
$0 + 0 + 0 \equiv 0
$0 + 0 + 1 \equiv 1
$0 + 0 + 4 \equiv 4
$0 + 1 + 1 \equiv 2
$0 + 1 + 4 \equiv 5
$0 + 4 + 4 \equiv 0 (mod 8)
$1 + 1 + 1 \equiv 3
$1 + 1 + 4 \equiv 6
$1 + 4 + 4 \equiv 1 (mod 8)
$4 + 4 + 4 \equiv 4 (mod 8)
Hence, the achievable totals are 0 through 6 modulo 8, but never 7 modulo 8, since no combination yields this residue.[11] Such modular obstructions are essential in analyzing representability by sums of squares.
Statement
The Theorem
Legendre's three-square theorem provides a complete characterization of the natural numbers that can be expressed as the sum of three squares of integers. Specifically, a natural number n \geq 1 can be written as n = x^2 + y^2 + z^2 for some integers x, y, z if and only if n is not of the form $4^a (8b + 7) where a and b are nonnegative integers.[13]This forbidden form $4^a (8b + 7) arises by iteratively dividing n by 4 until the quotient is odd, at which point the resulting odd number must not be congruent to 7 modulo 8; here, a counts the number of factors of 4 removed, and b is such that $8b + 7 captures the residue class modulo 8.[14]
Characterization of Representable Numbers
The characterization of natural numbers representable as the sum of three squares of integers centers on an explicit arithmetic condition derived from Legendre's theorem. Specifically, a natural number n can be expressed as n = x^2 + y^2 + z^2 for integers x, y, z if and only if n is not of the form $4^k (8\ell + 7) for nonnegative integers k, \ell.[15] This condition involves iteratively removing factors of 4 from n until an odd integer remains, after which that odd part must not be congruent to 7 modulo 8.[16] Equivalently, after dividing out the highest power of 4 dividing n, the resulting odd integer must belong to one of the residue classes 1, 3, or 5 modulo 8.[15]This criterion implies that the set of representable numbers has a positive asymptotic density within the natural numbers. In particular, the proportion of natural numbers up to X that can be written as a sum of three squares approaches $5/6 as X \to \infty, as established by classical analytic methods accounting for the modular obstructions.[17] This density reflects the fact that, modulo 8, squares take values 0, 1, or 4, and sums of three such values avoid only the residue 7, with adjustments for powers of 2 yielding the overall $5/6 limit.[17]In terms of prime factorization, the representability condition does not reduce to a simple rule like that for sums of two squares, where primes congruent to 3 modulo 4 must appear to even powers. Instead, primes of the form $4m + 3 may occur to odd powers in representable numbers, provided the overall form after removing factors of 4 avoids congruence to 7 modulo 8; for instance, primes themselves congruent to 7 modulo 8 cannot be represented, but those congruent to 3 modulo 8 can.[15] There is no complete prime-based criterion analogous to the two-squares case, as the obstruction depends on the global modular structure rather than individual exponents alone.[18] This complexity arises from the underlying quadratic form theory, where the theorem follows from the Hasse-Minkowski principle, ensuring local solubility over the reals and all p-adic fields implies global representability for the ternary form x^2 + y^2 + z^2 - n w^2.[18]
History
Early Contributions
The study of sums of squares in number theory began in the 17th century with Pierre de Fermat, who laid foundational claims through correspondence. In a letter to Marin Mersenne dated December 25, 1640, Fermat stated that every prime number of the form $4k + 1 (along with 2) can be expressed as the sum of two integer squares, and moreover, that every natural number is the sum of at most four squares. These assertions, made without proof, sparked extensive investigation into representations by quadratic forms. Fermat further observed in correspondence that no integer congruent to 7 modulo 8 can be written as the sum of three integer squares, using modular considerations to support this impossibility.Leonhard Euler significantly advanced this area during the 18th century through his systematic study of quadratic forms and their representations. In a 1749 paper, Euler provided the first published proof of Fermat's theorem on sums of two squares, employing techniques from continued fractions and the arithmetic of Gaussian integers, though without explicitly introducing them. Euler's broader work on binary and ternary quadratic forms, detailed in his contributions to the Novi Commentarii Academiae Scientiarum Petropolitanae and other publications, explored the conditions under which numbers could be represented by such forms, including partial results on sums of three squares. He extended Fermat's modular observation by proving that no number of the form $4^a(8k + 7), where a \geq 0 and k \geq 0, can be expressed as the sum of three integer squares, using inductive arguments on the power of 4.[19]In 1774, Nicolas von Béguelin contributed empirical observations to the discussion of three squares in a memoir presented to the Berlin Academy. He noted specifically that numbers like 7 and 15 cannot be written as the sum of three integer squares and conjectured, based on computational checks, that every positive integer congruent to 1, 2, 3, 5, or 6 modulo 8 is representable in this way, though he offered no general proof. This work appeared in the Nouveaux Mémoires de l'Académie Royale des Sciences et Belles-Lettres de Berlin under the title "Démonstration du théorème de Bachet, et analyse des nombres en triangulaires & en quarrés," linking sums of squares to triangular numbers but leaving the sufficiency unestablished.[20]
Legendre's Proof and Subsequent Developments
Adrien-Marie Legendre provided the first complete proof of the three-square theorem in his 1798 publication Essai sur la théorie des nombres, where he established the precise condition under which a natural number can be expressed as the sum of three integer squares.[21] This work built on earlier partial results and conjectures, marking a significant advancement in the study of quadratic forms and Diophantine representations.[22]In 1801, Carl Friedrich Gauss independently confirmed Legendre's result and offered a refined proof in his seminal Disquisitiones Arithmeticae, presenting it in Articles 291–292 as a cornerstone of ternary quadratic form theory.[23] Gauss's approach emphasized the connection to class numbers and modular arithmetic, providing greater clarity and integrating the theorem into a broader framework of number-theoretic identities.Augustin-Louis Cauchy proved Fermat's polygonal number theorem in 1813, noting that Legendre's three-square theorem is equivalent to the statement for squares in this general result.[24] This contribution made the result more accessible and influenced subsequent pedagogical treatments.Peter Gustav Lejeune Dirichlet delivered an elegant analytic proof in 1850, relying on three key lemmas concerning the representation properties of binary and ternary quadratic forms. His method avoided descent techniques, instead leveraging density arguments and the distribution of quadratic residues to establish the theorem's sufficiency.In the 20th century, Hermann Minkowski's development of the geometry of numbers provided a geometric perspective that inspired later proofs, such as N.C. Ankeny's 1957 application of Minkowski's convex body theorem to confirm the three-square theorem via lattice point estimates.
Proofs
Necessity: The "Only If" Direction
To establish the necessity direction of Legendre's three-square theorem, it must be shown that if a positive integer n can be expressed as the sum of three integer squares, n = x^2 + y^2 + z^2 for some integers x, y, z, then n cannot be of the form $4^a (8b + 7) where a \geq 0 and b \geq 0 are integers.The proof begins by considering the case where a = 0, so n = 8b + 7. Assume for contradiction that n \equiv 7 \pmod{8} and n = x^2 + y^2 + z^2. The possible residues of squares modulo 8 are 0 (for x \equiv 0 \pmod{4}), 1 (for odd x), or 4 (for x \equiv 2 \pmod{4}).[25] No combination of three such residues sums to 7 modulo 8: the maximum sum is $4 + 4 + 4 = 12 \equiv 4 \pmod{8}, while other combinations yield 0, 1, 2, 3, 4, 5, or 6, but never 7. Thus, the equationx^2 + y^2 + z^2 \equiv 7 \pmod{8}has no integer solutions.For a \geq 1, suppose n = 4^a (8b + 7) = x^2 + y^2 + z^2. Then n is divisible by 4, so x, y, z must all be even (since an odd square is $1 \pmod{4} and at most one odd square would make the sum $1 or $3 \pmod{4}, while two odd squares sum to $2 \pmod{4}, none of which is $0 \pmod{4}). Write x = 2x_1, y = 2y_1, z = 2z_1, yielding n/4 = x_1^2 + y_1^2 + z_1^2 = 4^{a-1} (8b + 7). Repeating this process a times reduces to the case where the remaining factor is $8b + 7, which cannot be a sum of three squares by the modulo 8 argument. Therefore, no such x, y, z exist.
Sufficiency: The "If" Direction
The sufficiency direction asserts that a natural number n not of the form $4^k(8m + 7) for nonnegative integers k, m can be expressed as n = a^2 + b^2 + c^2 for some integers a, b, c.[18] An elementary proof begins by reducing to the case of odd n \not\equiv 7 \pmod{8}. Write n = 4^k m where m is odd and m \not\equiv 7 \pmod{8}; if m = a^2 + b^2 + c^2, then n = (2^k a)^2 + (2^k b)^2 + (2^k c)^2.[14]For odd n \not\equiv 7 \pmod{8}, first consider primes p = 2 or p \equiv 1, 3, 5 \pmod{8}. Here, $2 = 1^2 + 1^2 + 0^2; primes p \equiv 1 \pmod{4} are sums of two squares by Fermat's theorem, hence p = x^2 + y^2 + 0^2; and primes p \equiv 5 \pmod{8} or p \equiv 3 \pmod{8} admit explicit representations as sums of three squares via descent arguments on quadratic residues.[14] To extend to composites, use the identity(a^2 + b^2 + c^2)(d^2 + e^2 + f^2) = (ad - be - cf)^2 + (ae + bd + cf)^2 + (af - cd + be)^2,which holds for all integers a, b, c, d, e, f and shows that the set of sums of three squares is closed under multiplication.[26] Thus, any such n, factored into primes each representable as a sum of three squares, is itself representable.[14]Dirichlet provided an alternative proof in 1850 using the theory of positive definite ternary quadratic forms.[27] He established three key lemmas: first, that for n \not\equiv 7 \pmod{8}, there exists a ternary form of discriminant -4n representing 1; second, that all such forms equivalent to one representing 1 can be reduced to a standard form; and third, that the principal form x^2 + y^2 + z^2 (of discriminant -4) is the unique reduced form of determinant 1, implying n is represented by it after equivalence transformations on composites via composition laws.[28] This approach avoids explicit factorizations but relies on reduction theory for quadratic forms.[27]A modern proof employs the Hasse-Minkowski theorem, which states that a quadratic form over \mathbb{Q} represents 0 nontrivially if and only if it does so over \mathbb{R} and every \mathbb{Q}_p.[18] Consider x^2 + y^2 + z^2 - n t^2 = 0; over \mathbb{R}, solutions exist for n > 0; over \mathbb{Q}_p for odd p, the form is isotropic; and over \mathbb{Q}_2, isotropy holds precisely when n \not\equiv 7 \pmod{8} after scaling by powers of 4.[18] A nontrivial rational solution yields integer solutions via clearing denominators, completing the proof.[18]
Related Results
Connection to Lagrange's Four-Square Theorem
Lagrange's four-square theorem, established in 1770, asserts that every natural number can be expressed as the sum of four integer squares.[29] This result builds upon earlier work, including Euler's discovery of the four-square identity, which states that the product of two sums of four squares is itself a sum of four squares:(a^2 + b^2 + c^2 + d^2)(e^2 + f^2 + g^2 + h^2) = (ae - bf - cg - dh)^2 + (af + be + ch - dg)^2 + (ag - bh + ce + df)^2 + (ah + bg - cf + de)^2.This identity enables the multiplicative property essential for extending representations from primes to composite numbers.[30]Legendre's three-square theorem provides a refinement by identifying precisely which natural numbers can be written as a sum of three squares—namely, those not of the form $4^a(8b + 7) for nonnegative integers a and b.[31] For such representable numbers, Lagrange's theorem follows immediately by appending a fourth square of zero. The exceptional cases, which require four nonzero squares, are thus isolated, allowing proofs of the four-square theorem to focus on handling these specific forms.[20]In particular, any number of the form $4^a(8b + 7) reduces via $4^a = (2^a)^2 + 0^2 + 0^2 + 0^2 to the core problem of representing $8b + 7 as a sum of four squares.[29] Euler's identity facilitates this by confirming that primes congruent to 7 modulo 8, which cannot be sums of three squares, can nonetheless be expressed as sums of four squares, with the property propagating multiplicatively to all such exceptions.[30] This targeted approach, leveraging the characterization from Legendre's theorem, fills the gaps and confirms the universality of four squares.[20]
Generalizations and Other Theorems
The Hasse–Minkowski theorem establishes the local–global principle for quadratic forms over the rational numbers \mathbb{Q}, stating that a quadratic form in n \geq 5 variables over \mathbb{Q} (or certain forms in fewer variables) represents zero nontrivially if and only if it does so over the real numbers \mathbb{R} and over every p-adic field \mathbb{Q}_p for primes p. This principle underlies Legendre's three-square theorem, as the representability of a positive integer by the ternaryquadratic form x^2 + y^2 + z^2 over \mathbb{Z} aligns with the global condition over \mathbb{Q} once local solvability (particularly the 2-adic obstruction excluding forms $4^a(8b + 7)) is satisfied.[32]A direct generalization of Legendre's theorem to the field of rational numbers \mathbb{Q} characterizes which positive rationals are sums of three rational squares: a positive rational r can be expressed as r = x^2 + y^2 + z^2 with x, y, z \in \mathbb{Q} if and only if r is not of the form $4^\alpha (8\beta + 7) \gamma^2, where \alpha, \beta \in \mathbb{N} \cup \{0\} and \gamma > 0 is rational. This condition mirrors the integer case, arising solely from the local obstruction in the 2-adic numbers \mathbb{Q}_2, while representations are possible over \mathbb{R} and all odd \mathbb{Q}_p.[33]Other landmark theorems on sums of squares provide contrasting bounds on the number of terms needed. Fermat's two-square theorem asserts that an odd prime p is the sum of two integer squares if and only if p \equiv 1 \pmod{4}; more generally, a positive integer is a sum of two integer squares precisely when every prime congruent to 3 modulo 4 divides it to an even power.[34] Lagrange's four-square theorem extends this by showing that every natural number is the sum of four integer squares, resolving the question for arbitrary dimensions up to four. As a consequence, every natural number is also a sum of five integer squares.[35]The three-square theorem also connects to the class number problem in imaginary quadratic fields. The relevant discriminants -4n for which the class number of the order of conductor 2 in \mathbb{Q}(\sqrt{-n}) is one occur precisely when n = 1, 2, 3, 4, 7; this structural property ensures that the genus of positive definite ternary quadratic forms of determinant 1 consists of a single equivalence class, namely the sum-of-three-squares form, which is essential for the theorem's proof via reduction theory and Minkowski's geometry of numbers.[36]
Examples
Numbers That Cannot Be Expressed as Three Squares
Legendre's three-square theorem establishes that a natural number cannot be expressed as the sum of three squares if and only if it is of the form $4^a(8b+7) for nonnegative integers a and b.[1] These forbidden numbers illustrate the theorem's necessity condition, as squares modulo 8 are 0, 1, or 4, so the sum of three squares modulo 8 can only be 0 through 6, never 7; repeatedly dividing by 4 (which preserves the property if a>0) eventually yields a number congruent to 7 modulo 8.[28]Specific examples highlight this form. The number 7 is $4^0(8\cdot0+7), and $7 \equiv 7 \pmod{8}, so it cannot be written as x^2 + y^2 + z^2 for integers x,y,z.[1] Similarly, 15 is $4^0(8\cdot1+7), with $15 \equiv 7 \pmod{8}; 23 is $4^0(8\cdot2+7), with $23 \equiv 7 \pmod{8}; and 31 is $4^0(8\cdot3+7), with $31 \equiv 7 \pmod{8}. For 28, dividing by $4^1 gives 7, which is $8\cdot0+7 and congruent to 7 modulo 8, confirming no such representation exists.[37] In each case, the necessity proof of the theorem guarantees the absence of integer solutions.[1]The pattern extends to all numbers of this form, with no exceptions. Up to 100, these include: 7, 15, 23, 28, 31, 39, 47, 55, 60, 63, 71, 79, 87, 92, 95.[37] This sequence, known as OEIS A004215, catalogs all such forbidden numbers and underscores their infinite density while adhering strictly to the $4^a(8b+7) structure.[37]
Numbers That Can Be Expressed as Three Squares
Legendre's three-square theorem guarantees that any natural number not of the forbidden form can be expressed as the sum of three integer squares, and this section illustrates the sufficiency part through explicit decompositions.[28]For the smallest positive integers, the representations are straightforward. For instance, $1 = 1^2 + 0^2 + 0^2, $2 = 1^2 + 1^2 + 0^2, and $3 = 1^2 + 1^2 + 1^2. Similarly, $6 = 2^2 + 1^2 + 1^2 and $10 = 3^2 + 1^2 + 0^2. These decompositions can be verified directly by computation.[28]Larger examples further demonstrate the theorem's applicability. The number 11, which is congruent to 3 modulo 8, admits the representation $11 = 3^2 + 1^2 + 1^2. Likewise, 14 can be written as $14 = 3^2 + 2^2 + 1^2. Such expressions confirm that numbers satisfying the theorem's condition indeed possess three-square decompositions.[28]Some numbers allow multiple distinct representations, highlighting the non-uniqueness of the decomposition in general. For example, $50 = 5^2 + 5^2 + 0^2 = 7^2 + 1^2 + 0^2, providing two different ways to express it as a sum of three squares (up to ordering and signs).[28]To find these representations, one approach for small numbers is trial and error: systematically test non-negative integers a \geq b \geq c \geq 0 until a^2 + b^2 + c^2 = n. For larger numbers, more efficient methods rely on mathematical identities or algorithmic constructions derived from proofs of the theorem, such as Dirichlet's composition of quadratic forms.[28]