Sums of Two Squares
Which whole numbers can be written as a sum of two perfect squares? The number is, since . So is . But , , and are not, no matter how one tries. This simple-looking question is decided by nothing more exotic than divisibility by primes of a certain congruence class. Yet it turns out to encode deep structure. That structure connects elementary number theory, the arithmetic of the Gaussian integers, and the theory of quadratic forms. The idea matters because it is the first genuinely arithmetic classification theorem. A global property of an integer — being a sum of two squares — is shown to be equivalent to a purely local, prime-by-prime condition. This local-to-global philosophy means checking the primes, then assembling the answer. It recurs throughout algebraic number theory, from Hasse's local-global principle to the modern theory of -functions. Fermat's discovery that a prime is a sum of two squares exactly when (or ) is the seed. From it grows the entire theory of representations by quadratic forms. The big picture is that , the ring of Gaussian integers, behaves almost exactly like . It has a division algorithm, unique factorization, and primes. A rational prime either stays prime in , or splits into a product of two conjugate Gaussian primes. This splitting behavior is governed precisely by whether is a quadratic residue mod . That question is already answered by the Supplementary Laws of quadratic reciprocity. Sums of two squares are norms of Gaussian integers. So the multiplicativity of the norm converts a question about representability into a question about factorization. This article proves Fermat's two-square theorem via Gaussian integers and unique factorization. It states and proves the full characterization of sums of two squares for arbitrary integers. It also gives an alternative descent-style argument for the prime case. It closes by pointing toward the natural generalizations. These include sums of four squares (where every integer succeeds) and the general theory of Binary Quadratic Forms.
- Fermat's theorem: an odd prime is a sum of two squares iff .
- The proof uses the Gaussian integers , a unique factorization domain where the norm is multiplicative.
- splits in because is then a quadratic residue mod ; stays prime.
- The general theorem: is a sum of two squares iff every prime divides to an even power.
- The set of sums of two squares is closed under multiplication — the Brahmagupta–Fibonacci identity is the algebraic shadow of .
- Key idea: representability by is a local, prime-by-prime condition, made transparent by working in the ring instead of .
Where this lives today
The splitting behavior of primes in — inert, split, or ramified according to congruence mod — is the prototype for the general theory of splitting of primes in Binary Quadratic Forms and quadratic number fields, itself a special case of class field theory describing how primes factor in arbitrary Galois extensions. In cryptography, fast algorithms for expressing a prime as a sum of two squares (via continued fractions or Cornacchia's algorithm) are used in elliptic-curve point-counting and in constructing curves with prescribed complex multiplication. In computational number theory, the sum-of-two-squares theorem underlies efficient primality-related tests and factorization heuristics tied to Gaussian integer arithmetic. In physics and combinatorics, the function counting representations of as an ordered sum of two squares (with signs) appears in lattice point counting problems, the Gauss circle problem, and the theory of modular forms, where is essentially a Fourier coefficient of a weight-one theta series. The broader technique — passing to a larger ring with unique factorization to resolve a Diophantine question in — reappears throughout algebraic number theory, and directly foreshadows Lagrange's theorem on Sums of Four Squares, where every nonnegative integer succeeds because the relevant ring (Hurwitz quaternions) has no analogous obstruction from inert primes.
:::quiz[Why does being a UFD matter in the proof that implies is a sum of two squares?]
- It guarantees that irreducibles are prime, letting us conclude or from ✓
- It is only needed to define the norm function
- It ensures has infinitely many units
- It is irrelevant; the result only uses quadratic reciprocity
:::quiz[What role does staying irreducible in play in the general theorem?]
- It forces the power of in any norm to be even, since and contributes in matched pairs ✓
- It shows can never divide any Gaussian integer
- It implies is always a sum of two squares itself
- It has no role; only split primes matter
:::faq[What breaks if we replace with a non-UFD analog, like ?] The entire argument leans on unique factorization to conclude that if a prime divides a product it divides one of the factors. In , unique factorization fails: gives two genuinely different factorizations into irreducibles. Consequently the clean splitting dichotomy (inert vs. split, controlled by a single congruence) does not directly characterize representability by ; one instead needs the full machinery of class numbers and genus theory for binary quadratic forms of discriminant , which is a strictly harder, richer subject building on this simpler UFD case.
Ask the tutor: Can you show me step-by-step how to find with or prove it's impossible?
A nonnegative integer is called a sum of two squares if there exist integers (possibly zero or negative, only matter) with
The Gaussian integers form the ring
The norm is defined by
A rational integer is a sum of two squares if and only if for some .
Intuition
The norm turns multiplication in into multiplication in : , because complex conjugation is a ring homomorphism, so . This one identity, unpacked in coordinates, is the classical Brahmagupta–Fibonacci identity:
It shows immediately that the set of sums of two squares is closed under multiplication. The subtler direction — which primes are sums of two squares, and what happens when we divide — needs the arithmetic of itself. Picture as the lattice of Gaussian integers in the plane. The norm of a lattice point is the squared Euclidean distance to the origin, so " is a sum of two squares" literally means some lattice point lies at squared-distance from the origin. A prime of ordinary arithmetic can behave in two ways inside this lattice: it can remain irreducible (a "Gaussian prime" with no smaller lattice-factors), or it can factor as into two conjugate Gaussian primes, in which case is visibly a sum of two squares. Whether splits or stays prime is decided by a single congruence condition mod 4, which is exactly the statement that is or is not a quadratic residue mod .
Central results
Let be an odd prime. Then is a sum of two squares, i.e. for some integers , if and only if
For any with , there exist with
Consequently is a Euclidean domain, hence a principal ideal domain, hence a unique factorization domain. Its units are exactly .
Division algorithm. Write with . Choose integers with and ; set . Then
Let , so and , giving .
Consequences. A Euclidean domain is a principal ideal domain: given an ideal , pick nonzero of minimal norm; the division algorithm shows every satisfies with (since and is an ideal) and , forcing by minimality; hence . Every PID is a UFD, by the standard argument that in a PID irreducibles are prime (if is irreducible and but , then since the only divisors of are units and associates, so by Bézout in the PID there are with , giving , divisible by ) and every element factors into irreducibles by strong induction on the norm (if is not a unit and not irreducible, with , both , so induct).
Units. If is a unit, with both factors positive integers, so , i.e. , forcing , i.e. . Conversely each of these has an inverse in , so these are exactly the units.
∎:::lemma[Splitting criterion for rational primes in ] Let be an odd prime. (a) If , then is not irreducible in : it factors as for some Gaussian prime with . (b) If , then remains prime (irreducible) in .
By the Supplementary Laws (specifically the first supplement to quadratic reciprocity), is a quadratic residue mod if and only if .
(a) Suppose . Then there exists with , i.e. in . If were irreducible in , then since is a UFD, irreducibles are prime, so would divide or in . But would mean , forcing , absurd since ; likewise for . So is not irreducible in . Since is not a unit (as ) and not irreducible, write with non-units. Taking norms, with (non-units have norm , and norm is impossible since ), so the only possibility is . Write ; then , and , having the same norm and satisfying , must equal up to a unit (concretely ). So . Finally is irreducible: if nontrivially then prime forces one factor to have norm , a unit — contradiction to nontriviality. So is a Gaussian prime.
(b) Suppose and suppose for contradiction is not irreducible in . As in (a), with non-units, forcing . Write , so . Reducing mod 4: squares mod 4 are or , so , never . But , a contradiction. Hence is irreducible in .
∎(Proof of Fermat's Two-Square Theorem, using the two lemmas.) () Suppose . By the splitting lemma (a), for some integers . This is exactly the statement that is a sum of two squares. () Suppose is odd and for integers . Reducing mod 4, . Since is odd, is odd, so exactly one of is even and the other odd; hence . So . Both directions established; the theorem is proved.
∎The direction just given () is a two-line congruence argument requiring no machinery; the substantive direction is the existence of a representation when , which is where unique factorization in does the real work.
A positive integer is a sum of two squares if and only if, writing
in prime factorization, every exponent (on primes ) is even.
We use that and, for , in (splitting lemma), while for , stays prime in (splitting lemma).
() Sufficiency. If every is even, write where is (by the Brahmagupta–Fibonacci identity, applied repeatedly, since and each are individually sums of two squares) itself a sum of two squares, say . Since each is even, is a perfect square. Then
a sum of two squares.
() Necessity. Suppose for some , and let be a prime dividing ; we must show divides to an even power. Let and write , with ; let correspond to the power of dividing coming from . More directly: suppose exactly divides... it is cleaner to argue as follows. Suppose, for contradiction, that divides to an odd power . Consider , so . Since is inert (irreducible, and prime, by the splitting lemma) in , factor into Gaussian primes:
where is a unit, is the exact power of the rational prime dividing in (possible since is a Gaussian prime, so we may speak of -adic valuation on ), and each is a Gaussian prime not associate to . Taking norms,
using since is a rational integer. Since is a rational prime and none of the is associate to , none of the primes dividing 's or 's norm can equal arising from this factor in a way that contributes an odd extra power — more precisely, since for each (else would be an associate of , both being Gaussian primes with forcing and to generate the same prime ideal), and is a rational prime, (if , then since is a Gaussian prime, or , contradicting non-associateness after conjugating if needed). So the exact power of dividing in is precisely , an even number. This contradicts our assumption that is odd. Hence must be even for every prime dividing .
∎Worked examples
Example 1: . Factor: . Check congruences: and (since ); there are no prime factors at all, so vacuously every such exponent is even. By the corollary, is a sum of two squares. To find one explicitly, factor in : and (check: ✓.) Then
Group as with . Compute . Then
So . Indeed . A second representation comes from pairing differently: take — but is real, giving , norm , i.e. . Both check out: and . (The multiplicity of representations reflects the number of ways to distribute conjugate factors among the two "sides" — a phenomenon fully explained by Binary Quadratic Forms.)
Example 2: . Factor: . Here the only odd prime factors are and , both (since , ), and both appear to an even power ( and respectively). By the corollary, is a sum of two squares — but since is itself a perfect square, the trivial representation exists. Are there nontrivial ones? Since has no prime factor , the number of essentially distinct representations (up to order/sign) is exactly one — the trivial one — a fact explained by the formula for the number of representations in terms of divisors of congruent to and mod . Direct check: one can verify computationally that no with satisfy besides using . Contrast: , both primes to the first (odd) power. By the corollary, is not a sum of two squares — consistent with direct search: , none a perfect square.
The theorem crucially uses the modulus ; the analogous statement is false if one tries the naive guess "a prime is a sum of two squares iff " or any other modulus not tied to being a QR. Consider . We have (since ), so a false analogous theorem based on mod would predict is a sum of two squares. But checking all cases: , none are perfect squares, so is not a sum of two squares. Indeed , correctly predicting failure via the true (mod ) criterion. This shows the hypothesis "" cannot be replaced by an arbitrary congruence condition: it is special because is precisely the modulus controlling whether is a quadratic residue mod (via the first supplementary law), which is exactly the condition that makes solvable and hence reducible in . Swapping in an unrelated modulus severs this connection and the theorem simply fails.
Scope, edge cases & common traps
| Situation | What happens | Why |
|---|---|---|
| , always a sum of two squares | is ramified, not inert or split, in | |
| a perfect square | Always a sum of two squares (, or nontrivially if it has prime factors ) | Trivial representation always available |
| , sum of two squares by convention | Degenerate but consistent case | |
| Negative "sums" | Not considered; squares are | Definition restricted to |
| to an odd power dividing | is never a sum of two squares | Proven via inert primes in |
| Composite with mixed factors | Must check every prime individually, not just one | A single bad odd-power prime factor ruins representability |
The mathematician behind it
Origins · 1 minPierre de Fermat and how this was discovered
Fermat announced the two-square theorem in a 1640 letter to Mersenne without publishing a proof, characteristic of his style of stating results and daring correspondents to find demonstrations. He claimed a proof by his method of infinite descent, though no written proof by Fermat survives; the first published proofs came later, notably by Euler. Fermat's parallel investigations into representability by , , and his famous Last Theorem all belong to the same broader program of understanding which integers arise from quadratic forms — a program that Euler, Lagrange, Gauss, and eventually Dedekind and Hilbert would turn into modern algebraic number theory.
Why does the modulus 4 appear rather than some other number?
The modulus arises because the question "is a quadratic residue mod " is answered by the first supplementary law of quadratic reciprocity, which states . This expression depends only on : it equals exactly when and when . Since solvability of is precisely what makes reducible in (via ), the congruence class mod is not a arbitrary choice but is forced by the structure of quadratic residues. No smaller or unrelated modulus captures this dichotomy correctly.
Does every integer of the form automatically become a sum of two squares?
No — this conflates a necessary condition for primes with a sufficient condition for all integers. For primes, is exactly equivalent to being a sum of two squares. For composite , one must factor completely: is neither necessary nor sufficient in general. For example is a sum of two squares only trivially (; also fails to have a nontrivial one since appears to an even power here, so it's actually fine — but fails entirely since and each appear to odd power). The correct test always requires prime factorization.
How many ways can a number be written as a sum of two squares?
There is an exact formula, due to Jacobi: if with and , then is a sum of two squares iff every is even, and in that case the number of representations (counting order and sign, i.e. , , all distinct) equals . This connects directly to Binary Quadratic Forms, where is computed via class numbers and divisor-counting arguments refining the qualitative theorem given here.
Is the representation of a prime as a sum of two squares unique?
Yes, up to order and sign. If with , then , and since with a Gaussian prime, unique factorization in forces to be a unit multiple of or . The four units only permute signs and swap , so as unordered pairs. This uniqueness is special to primes; composite with several prime factors typically admit multiple genuinely different representations, as seen with above.
How does this connect to sums of four squares?
Lagrange's four-square theorem states every nonnegative integer is a sum of four squares, with no congruence obstruction whatsoever — a striking contrast to the two-square case. The underlying reason is that the relevant "integers with norm," the Hurwitz quaternions, form a noncommutative analog of a Euclidean domain with enough units and enough flexibility that no prime is ever "inert" in the same obstructive sense; every prime factors compatibly. The two-square theorem is thus the delicate, obstruction-laden special case, while Sums of Four Squares is the unconditional generalization, and both sit inside the broader landscape of Binary Quadratic Forms and quaternionic arithmetic.