Which primes are a sum of two squares?

May 31, 2026 · math, number-theory

x2+y2x^2+y^2 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. 5=12+225 = 1^2+2^2. 13=22+3213 = 2^2+3^2. 17=12+4217=1^2+4^2. 29=22+5229=2^2+5^2. But 77? No combination of two squares gives 77. Neither does 1111, 1919, or 2323. Something is sorting the primes into two piles, and if you list enough of them the pattern jumps out:

5,13,17,29,37,41,sums of two squares3,7,11,19,23,31,not\underbrace{5, 13, 17, 29, 37, 41, \ldots}_{\text{sums of two squares}} \qquad \underbrace{3, 7, 11, 19, 23, 31, \ldots}_{\text{not}}

The first list is exactly the primes congruent to 1mod41 \bmod 4; the second is exactly the primes congruent to 3mod43 \bmod 4 (setting p=2=12+12p=2=1^2+1^2 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 3(mod4)\equiv 3 \pmod 4 never work

This direction is a clean congruence argument. Look at squares modulo 4. Every integer is 0,1,2,0,1,2, or 3(mod4)3 \pmod 4, and squaring gives:

020,121,220,321(mod4).0^2\equiv 0,\quad 1^2\equiv 1,\quad 2^2\equiv 0,\quad 3^2\equiv 1 \pmod 4.

So a perfect square is always 00 or 1(mod4)1 \pmod 4 — never 22 or 33. Now suppose p=x2+y2p = x^2+y^2. Each of x2,y2x^2,y^2 is 00 or 1(mod4)1 \pmod 4, so their sum is 0,1,0,1, or 2(mod4)2 \pmod 4. It is never 3(mod4)3 \pmod 4. Hence no prime p3(mod4)p\equiv 3\pmod4 (indeed no integer 3(mod4)\equiv 3\pmod4) can be a sum of two squares. Done — completely elementary, no gaps.

That leaves the real question: why does every prime p1(mod4)p\equiv1\pmod4 succeed?

Fermat’s two-square theorem

Claim. An odd prime pp is a sum of two squares if and only if p1(mod4)p\equiv1\pmod4.

We’ve proved the “only if.” For the “if” direction we need two ingredients.

Ingredient 1: 1-1 is a quadratic residue mod pp exactly when p1(mod4)p\equiv1\pmod4.

The multiplicative group (Z/pZ)×(\mathbb{Z}/p\mathbb{Z})^\times is cyclic of order p1p-1 (a standard fact about the nonzero residues modulo a prime — every finite subgroup of the multiplicative group of a field is cyclic). Let gg be a generator. Every nonzero residue is gkg^k for some kk, and gkg^k is a square iff kk is even (since g2j=(gj)2g^{2j}=(g^j)^2, and conversely if gk=(gm)2=g2mg^k=(g^m)^2=g^{2m} then k2m(modp1)k\equiv 2m \pmod{p-1}, which forces kk even when p1p-1 is even — true since pp is odd). Now, 1-1 has order exactly 22 in this group (it squares to 11 and isn’t 11 itself, for p>2p>2), and in a cyclic group of order p1p-1, the unique element of order 22 is g(p1)/2g^{(p-1)/2}. So 1=g(p1)/2-1 = g^{(p-1)/2}, and 1-1 is a square iff (p1)/2(p-1)/2 is even, i.e. iff p1(mod4)p\equiv1\pmod4.

(This is a special case of Euler’s criterion: for aa coprime to pp, aa is a quadratic residue mod pp iff a(p1)/21(modp)a^{(p-1)/2}\equiv1\pmod p. Plugging in a=1a=-1 and using (1)(p1)/2=±1(-1)^{(p-1)/2} = \pm1 recovers exactly the parity statement above.)

So when p1(mod4)p\equiv1\pmod4, there is an integer mm with

m21(modp),i.e.pm2+1.m^2 \equiv -1 \pmod p, \qquad\text{i.e.}\qquad p \mid m^2+1.

Ingredient 2: turning that divisibility into an actual representation.

Knowing pm2+1p\mid m^2+1 doesn’t yet hand you x,yx,y with p=x2+y2p=x^2+y^2 — it just says pp divides something of that shape. The bridge is a pigeonhole argument usually credited to Thue.

Consider all pairs (a,b)(a,b) with 0a,bp0\le a,b\le\lfloor\sqrt p\rfloor. There are (p+1)2>p(\lfloor\sqrt p\rfloor+1)^2 > p such pairs (squaring the floor-plus-one strictly exceeds pp). Look at the values ambmodpa - mb \bmod p over all these pairs. Since there are more than pp pairs and only pp residues mod pp, two different pairs (a1,b1)(a2,b2)(a_1,b_1)\neq(a_2,b_2) must give the same residue:

a1mb1a2mb2(modp).a_1 - m b_1 \equiv a_2 - m b_2 \pmod p.

Set x=a1a2x = a_1-a_2 and y=b1b2y=b_1-b_2 (not both zero, since the pairs are distinct). Then xmy(modp)x \equiv my \pmod p, so

x2m2y2y2(modp)px2+y2.x^2 \equiv m^2y^2 \equiv -y^2 \pmod p \quad\Longrightarrow\quad p \mid x^2+y^2.

Now bound the size: since 0ai,bip0\le a_i,b_i\le\lfloor\sqrt p\rfloor, we have x,yp<p|x|,|y| \le \lfloor\sqrt p\rfloor < \sqrt p, so

0<x2+y2<2p0 < x^2+y^2 < 2p

(it’s strictly positive because x,yx,y aren’t both zero). A positive multiple of pp that’s less than 2p2p must equal pp itself. Hence

x2+y2=p.x^2+y^2 = p.

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 1mod41\bmod4 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

Z[i]={a+bi:a,bZ},i2=1.\mathbb{Z}[i] = \{a+bi : a,b\in\mathbb{Z}\}, \qquad i^2=-1.

Define the norm N(a+bi)=a2+b2N(a+bi) = a^2+b^2 (literally the squared length of a+bia+bi as a point in the plane). The norm is multiplicative:

N(zw)=N(z)N(w)N(zw) = N(z)N(w)

for z,wZ[i]z,w\in\mathbb{Z}[i]. This is a one-line check: if z=a+biz=a+bi, w=c+diw=c+di, then zw=(acbd)+(ad+bc)izw = (ac-bd)+(ad+bc)i, so

N(zw)=(acbd)2+(ad+bc)2.N(zw) = (ac-bd)^2+(ad+bc)^2.

Expand both sides:

(acbd)2+(ad+bc)2=a2c22abcd+b2d2+a2d2+2abcd+b2c2=a2c2+b2d2+a2d2+b2c2=(a2+b2)(c2+d2)=N(z)N(w).\begin{aligned} (ac-bd)^2+(ad+bc)^2 &= a^2c^2-2abcd+b^2d^2+a^2d^2+2abcd+b^2c^2\\ &= a^2c^2+b^2d^2+a^2d^2+b^2c^2\\ &= (a^2+b^2)(c^2+d^2) = N(z)N(w). \end{aligned}

Now the question ”p=x2+y2p=x^2+y^2?” becomes “does pp factor in Z[i]\mathbb{Z}[i]?” Indeed, p=x2+y2p=x^2+y^2 is exactly p=(x+iy)(xiy)=N(x+iy)p = (x+iy)(x-iy) = N(x+iy). So pp being a sum of two squares means pp is the norm of a Gaussian integer, which is the same as saying pp splits into two (conjugate) factors in Z[i]\mathbb{Z}[i] instead of staying prime there.

This reframes Ingredient 1 exactly: if m21(modp)m^2\equiv-1\pmod p, then p(m+i)(mi)p \mid (m+i)(m-i) in Z[i]\mathbb{Z}[i]. But pp does not divide m+im+i or mim-i individually (their ratio m+imi=(m+i)2m2+1\frac{m+i}{m-i} = \frac{(m+i)^2}{m^2+1} is not a Gaussian integer, since m2+1m^2+1 has magnitude comparable to mm, not divisible cleanly). So pp divides a product without dividing either factor — which is impossible if pp is prime in Z[i]\mathbb{Z}[i] the same way ordinary primes behave (Euclid’s lemma). The only way out is that pp isn’t prime in Z[i]\mathbb{Z}[i] after all: it must factor, p=ππˉp = \pi\bar\pi, and taking norms gives p2=N(π)N(πˉ)p^2 = N(\pi)N(\bar\pi), forcing N(π)=N(πˉ)=pN(\pi)=N(\bar\pi)=p — which is exactly a representation p=x2+y2p=x^2+y^2.

For this “Euclid’s lemma” step to be legitimate, Z[i]\mathbb{Z}[i] 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 Z\mathbb{Z}), 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 Z[i]\mathbb{Z}[i] that needs a bespoke argument.

So the clean statement is: an odd prime pp splits in Z[i]\mathbb{Z}[i] iff p1(mod4)p\equiv1\pmod4; it stays prime (inert) iff p3(mod4)p\equiv3\pmod4. Congruence conditions controlling how primes split in a larger ring of integers — this turns out to be a completely general phenomenon, and x2+y2x^2+y^2 is the first example anyone meets.

What happens for x2+ny2x^2+ny^2?

The brief, elementary story above is special to n=1n=1. Ask the same question for x2+5y2x^2+5y^2: 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:

p=x2+5y2    p1,9(mod20),p = x^2+5y^2 \iff p\equiv 1,9 \pmod{20},

but there’s a second class of primes, p3,7(mod20)p\equiv3,7\pmod{20}, which are represented not by x2+5y2x^2+5y^2 itself but by the different-looking form 2x2+2xy+3y22x^2+2xy+3y^2 — a form of the same discriminant (20-20) that is genuinely inequivalent to x2+5y2x^2+5y^2 (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 11 (as it is for discriminant 4-4, corresponding to x2+y2x^2+y^2), 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 11 (discriminant 20-20 has class number 22), 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 x2+y2x^2+y^2 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 p,qp,q, define the Legendre symbol (pq)\left(\frac pq\right) to be +1+1 if pp is a quadratic residue mod qq, 1-1 if it’s a non-residue. Gauss’s law of quadratic reciprocity states:

(pq)(qp)=(1)p12q12.\left(\frac pq\right)\left(\frac qp\right) = (-1)^{\frac{p-1}{2}\cdot\frac{q-1}2}.

In words: whether pp is a square mod qq and whether qq is a square mod pp are almost always the same question, except when both pp and qq are 3mod43\bmod4, in which case the answers flip. This is exactly the kind of tool that generates congruence conditions like "p1(mod4)p\equiv1\pmod4" 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 x2+y2x^2+y^2), then a widening set of honest sketches of why the general question is harder (class groups for x2+ny2x^2+ny^2, 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 pp splits in such a ring — into two factors, staying inert, or (in bad cases) ramifying. Z[i]\mathbb{Z}[i] and Z\mathbb{Z} adjoin a root of x2+5x^2+5 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.