Which primes are a sum of two squares?
looks like about the simplest expression you could write down. So here’s a simple-looking question: which primes can be written as a sum of two squares?
Try a few. . . . . But ? No combination of two squares gives . Neither does , , or . Something is sorting the primes into two piles, and if you list enough of them the pattern jumps out:
The first list is exactly the primes congruent to ; the second is exactly the primes congruent to (setting aside as the one even prime). That such a clean congruence condition governs a question about sums of squares is not obvious at all, and chasing down why it’s true is a genuine on-ramp into a large piece of number theory: congruences, a new number system, unique factorization in that system, and eventually the machinery (class groups, quadratic reciprocity) needed to handle harder versions of the same question.
The easy half: primes never work
This direction is a clean congruence argument. Look at squares modulo 4. Every integer is or , and squaring gives:
So a perfect square is always or — never or . Now suppose . Each of is or , so their sum is or . It is never . Hence no prime (indeed no integer ) can be a sum of two squares. Done — completely elementary, no gaps.
That leaves the real question: why does every prime succeed?
Fermat’s two-square theorem
Claim. An odd prime is a sum of two squares if and only if .
We’ve proved the “only if.” For the “if” direction we need two ingredients.
Ingredient 1: is a quadratic residue mod exactly when .
The multiplicative group is cyclic of order (a standard fact about the nonzero residues modulo a prime — every finite subgroup of the multiplicative group of a field is cyclic). Let be a generator. Every nonzero residue is for some , and is a square iff is even (since , and conversely if then , which forces even when is even — true since is odd). Now, has order exactly in this group (it squares to and isn’t itself, for ), and in a cyclic group of order , the unique element of order is . So , and is a square iff is even, i.e. iff .
(This is a special case of Euler’s criterion: for coprime to , is a quadratic residue mod iff . Plugging in and using recovers exactly the parity statement above.)
So when , there is an integer with
Ingredient 2: turning that divisibility into an actual representation.
Knowing doesn’t yet hand you with — it just says divides something of that shape. The bridge is a pigeonhole argument usually credited to Thue.
Consider all pairs with . There are such pairs (squaring the floor-plus-one strictly exceeds ). Look at the values over all these pairs. Since there are more than pairs and only residues mod , two different pairs must give the same residue:
Set and (not both zero, since the pairs are distinct). Then , so
Now bound the size: since , we have , so
(it’s strictly positive because aren’t both zero). A positive multiple of that’s less than must equal itself. Hence
That’s the whole proof, and every step is elementary: cyclic group structure, pigeonhole, and a size bound. No case is swept under the rug.
Reframing it algebraically: the Gaussian integers
The proof above works, but it doesn’t yet explain why is the natural dividing line, beyond “that’s what the group-order computation says.” A cleaner conceptual picture comes from moving into a bigger number system: the Gaussian integers
Define the norm (literally the squared length of as a point in the plane). The norm is multiplicative:
for . This is a one-line check: if , , then , so
Expand both sides:
Now the question ”?” becomes “does factor in ?” Indeed, is exactly . So being a sum of two squares means is the norm of a Gaussian integer, which is the same as saying splits into two (conjugate) factors in instead of staying prime there.
This reframes Ingredient 1 exactly: if , then in . But does not divide or individually (their ratio is not a Gaussian integer, since has magnitude comparable to , not divisible cleanly). So divides a product without dividing either factor — which is impossible if is prime in the same way ordinary primes behave (Euclid’s lemma). The only way out is that isn’t prime in after all: it must factor, , and taking norms gives , forcing — which is exactly a representation .
For this “Euclid’s lemma” step to be legitimate, needs to be a unique factorization domain (UFD) — and it is, because it’s a Euclidean domain under the norm (you can always divide with a remainder of strictly smaller norm, the same mechanism that makes ordinary long division work for ), and Euclidean domains are always UFDs. I won’t reprove that general algebra fact here, but it’s the honest linchpin holding the whole “Gaussian integers” reframing together — take it as a standard, checkable result about Euclidean domains rather than something specific to that needs a bespoke argument.
So the clean statement is: an odd prime splits in iff ; it stays prime (inert) iff . Congruence conditions controlling how primes split in a larger ring of integers — this turns out to be a completely general phenomenon, and is the first example anyone meets.
What happens for ?
The brief, elementary story above is special to . Ask the same question for : which primes does it represent? You might hope for another single congruence condition, and you’d be disappointed. It turns out (I’ll state this without reproving it — the argument requires more machinery than fits here) that:
but there’s a second class of primes, , which are represented not by itself but by the different-looking form — a form of the same discriminant () that is genuinely inequivalent to (no integer change of variables turns one into the other), yet plays an analogous role.
This is the extra layer of complexity the single-prime, single-form case hides: for a fixed discriminant, there can be more than one genuinely inequivalent binary quadratic form, and a prime’s congruence class only tells you it’s represented by one of them — you need to know which. Gauss’s theory of binary quadratic forms organizes this by defining a group structure (composition of forms) on the equivalence classes of forms of a given discriminant — the class group. Its size, the class number, measures exactly how much “extra choice” there is: when the class number is (as it is for discriminant , corresponding to ), there’s only one form and you get a clean single congruence condition, which is exactly the lucky case Fermat’s theorem lives in. When the class number is bigger than (discriminant has class number ), the representability question splits across multiple forms and a single congruence no longer suffices. I’m not deriving class group composition here — it’s a real piece of 19th-century algebra in its own right — but naming what it measures is enough to see why was the easy case, not the typical one.
Quadratic reciprocity: the general machine
The congruence conditions above (mod 4, mod 20, …) are not arbitrary; they’re all instances of a single, much more general law. For odd primes , define the Legendre symbol to be if is a quadratic residue mod , if it’s a non-residue. Gauss’s law of quadratic reciprocity states:
In words: whether is a square mod and whether is a square mod are almost always the same question, except when both and are , in which case the answers flip. This is exactly the kind of tool that generates congruence conditions like "" from an underlying question about which primes divide values of a quadratic form — it’s the general engine; Fermat’s two-square theorem is one small, especially clean gear in it. Proving reciprocity itself is a substantial undertaking on its own (Gauss gave several different proofs across his life), and I’m not attempting it here — only placing it as the general law that the mod-4 condition above is a tiny special case of.
Where this really leads
The honest arc of this post is: one clean theorem, proved completely (Fermat/Euler/Thue on ), then a widening set of honest sketches of why the general question is harder (class groups for , reciprocity as the underlying machine). The natural home for all of this, if you keep going, is algebraic number theory: rings of integers of general number fields, and the question of how a prime splits in such a ring — into two factors, staying inert, or (in bad cases) ramifying. and adjoin a root of are the first, smallest examples of number rings; the general theory of prime splitting in number fields is exactly the generalization that Fermat’s two-square theorem was a two-thousand-year head start on, without anyone in the 17th century having the words for it yet.