Sums of Two Squares
Which whole numbers can be written as a^2 + b^2 for two whole numbers
a, b? Try a handful by hand: 2 = 1^2 + 1^2
works, 5 = 1^2 + 2^2 works, 13 = 2^2 + 3^2
works — but 3 refuses, and so do 7,
11 and 19, no matter how you juggle the
squares. It looks like a coin toss until you line the primes up and check the remainder each
one leaves when divided by 4 — and then a perfectly sharp pattern
snaps into focus.
Pierre de Fermat spotted that pattern in the
1640s and, in his usual style, announced it in a letter with a proof he never bothered to write
down. It took nearly a century for anyone else to nail it down properly — and when they did, the
reason turned out to run much deeper than a curiosity about squares: it is one of the first signs
that whole numbers and complex numbers are secretly talking to each other.
The prime case
An odd prime p is a sum of two squares if and only if
p \equiv 1 \pmod 4.
(The one even prime, 2 = 1^2 + 1^2, is always a sum of two squares too.)
So 5, 13, 17, 29, 37, 41 all split into two squares, while
3, 7, 11, 19, 23, 31 never do — however hard you search. The
"only if" half is genuinely easy: square any whole number and reduce it modulo
4, and you only ever get 0 or
1 (an even number squared is \equiv 0, an odd
number squared is \equiv 1). Add two such remainders and the only
possibilities are 0, 1, or 2 \pmod 4 —
3 \pmod 4 is simply never on the menu.
The "if" direction — that every prime \equiv 1 \pmod 4
really is achievable — is the deep part Fermat claimed but never showed. It leans on the fact that
-1 is a
quadratic residue
precisely when p \equiv 1 \pmod 4, which lets you engineer a multiple of
p that is itself a sum of two squares, then shrink it down step by step
until the multiple disappears.
Worked example: checking primes directly
Before trusting a theorem, test it by hand. Take 13: divide by
4 and the remainder is 1, so the theorem
promises a representation. Sure enough,
13 = 2^2 + 3^2 = 4 + 9.
And 29: 29 = 4 \cdot 7 + 1, so
29 \equiv 1 \pmod 4 — and indeed 29 = 2^2 + 5^2 = 4 + 25.
Now try the other side of the fence. Take 7: it is
\equiv 3 \pmod 4, so the theorem says no pair of squares can
ever add up to it. Check every possibility up to 7 —
0+7,\ 1+6,\ 4+3 — and none of those second numbers is itself a perfect
square. The same happens for 11 \equiv 3 \pmod 4: the candidate splits
are 1+10,\ 4+7,\ 9+2, and again nothing works. The theorem didn't just
predict failure — it explained why it was inevitable, before you ever picked up a pencil.
The full classification
Multiplicativity extends the prime case to every whole number. The key is Brahmagupta's
identity, discovered a thousand years before Fermat, which shows sums of two squares are
closed under multiplication:
(a^2+b^2)(c^2+d^2) = (ac-bd)^2 + (ad+bc)^2.
Multiply two sum-of-two-squares numbers together with this identity and the answer is automatically
another sum of two squares — no searching required, the new a, b just
fall out of the formula. Chase that idea through every prime factor and you land on the complete
rule:
A positive integer n is a sum of two squares if and only
if, in its prime factorisation, every prime \equiv 3 \pmod 4
appears to an even power.
- Primes \equiv 1 \pmod 4 may appear to any power at all — no restriction.
- The prime 2 may also appear to any power — no restriction.
- Only the primes \equiv 3 \pmod 4 are policed, and only their
exponent's parity matters.
Worked example: testing composite numbers
Take 45. Factorise it first:
45 = 3^2 \cdot 5.
The only prime \equiv 3 \pmod 4 present is 3,
and it appears to the power 2 — even. The rule says
45 must be a sum of two squares, and indeed
45 = 6^2 + 3^2 = 36 + 9.
Now try 21 = 3 \cdot 7. Both prime factors,
3 and 7, are \equiv 3 \pmod 4,
and each appears to the power 1 — odd. The rule predicts
failure, and a quick check of every split up to 21 confirms it: no pair
of squares sums to 21.
One more, to see the rule handle a busier number. Take 90 = 2 \cdot 3^2 \cdot 5.
The prime 2 is unrestricted, 5 \equiv 1 \pmod 4
is unrestricted, and the only policed prime, 3, again appears squared.
Sum of two squares it is:
90 = 9^2 + 3^2 = 81 + 9.
One way, or many?
A prime p \equiv 1 \pmod 4 has, in a precise sense, only
one way to split into two squares — swap a and
b, or flip a sign, and you're still describing the same pair. Check
13: the only positive solution is 2^2+3^2; no
other pair of positive whole numbers works.
Composite numbers can be more generous. Take 65 = 5 \cdot 13, a product
of two different primes \equiv 1 \pmod 4. It has
two genuinely different representations:
65 = 1^2 + 8^2 = 4^2 + 7^2.
Each extra distinct prime factor \equiv 1 \pmod 4 you multiply in tends
to unlock another representation — a direct echo of the Gaussian-integer splitting: every prime
that splits contributes a fresh way to pair up its complex factors.
Counting the representations exactly
There's even an exact formula for how many ways, due to Jacobi. Let
d_1(n) and d_3(n) count the divisors of
n that are \equiv 1 and
\equiv 3 \pmod 4 respectively.
The number of ways to write n as a^2+b^2,
counting different orders and signs of a, b as different, is
r_2(n) = 4\big(d_1(n) - d_3(n)\big).
Try it on 65: its divisors are 1, 5, 13, 65,
and every single one is \equiv 1 \pmod 4, so
d_1 = 4 and d_3 = 0, giving
r_2(65) = 16. That matches perfectly: the two unordered positive pairs
(1,8) and (4,7) each generate
8 signed, ordered variants (2 orderings times
2\times2 sign choices), for 8+8=16 in total.
A bonus: free Pythagorean triples
Every prime p = a^2+b^2 hands you a
Pythagorean
triple for free, no searching required. Since
(a^2-b^2)^2 + (2ab)^2 = (a^2+b^2)^2,
the triple (a^2-b^2,\ 2ab,\ a^2+b^2) always has legs and a hypotenuse
that are whole numbers, with the prime itself sitting as the hypotenuse. Feed in
5 = 1^2+2^2 (so a=2, b=1) and out drops the
smallest Pythagorean triple of all:
(2^2-1^2,\ 2\cdot2\cdot1,\ 2^2+1^2) = (3, 4, 5).
Try 13 = 3^2+2^2: it produces (3^2-2^2,\ 2\cdot3\cdot2,\ 13) = (5, 12, 13).
Every prime \equiv 1 \pmod 4 generates its own triple this way, all from
the same handful of algebra that Brahmagupta used to multiply sums of squares together.
It is tempting to shortcut the rule to "if a prime \equiv 3 \pmod 4
shows up anywhere in the factorisation, the number fails." That shortcut is wrong
— you saw 45 = 3^2 \cdot 5 succeed with 3
right there in the factorisation, because it appeared to an even power. The rule only cares
about the exponent's parity, not whether the prime is present at all.
The reverse trap is just as common: seeing a squared factor like 3^2 and
assuming that alone guarantees success, while forgetting to check every other
\equiv 3 \pmod 4 prime in the number. Take
99 = 3^2 \cdot 11. The 3 is safely squared —
but 11 (also \equiv 3 \pmod 4) appears only
to the power 1, and that single odd exponent is enough to disqualify the
whole number. 99 is not a sum of two squares, despite
its tidy-looking 3^2. You must check every qualifying prime, not
just the first one you notice.
In a 1640 letter to the monk and mathematician Marin Mersenne, Fermat announced the two-square
theorem as settled fact, complete with his trademark line that he had a proof by "infinite
descent" — and then, as usual, kept the details to himself. Mathematicians spent the better part of
a century trying to reconstruct it. It was
Leonhard Euler, working on and off for roughly seven
years, who finally nailed down a complete proof in 1747 — over a hundred years after Fermat's
letter, and eighty-two years after Fermat himself had died.
Here is the real reason the theorem is true, and it is far stranger than a fact about remainders.
Factor a^2+b^2 using imaginary numbers:
a^2+b^2 = (a+bi)(a-bi). Among the
Gaussian
integers — the grid of numbers a+bi — a prime
p \equiv 1 \pmod 4 stops being prime at all: it splits into two
genuinely different complex factors, exactly like 5 = (2+i)(2-i).
Multiply those two factors' "sizes" back together and you recover a^2+b^2
automatically. A prime \equiv 3 \pmod 4, by contrast, refuses to split —
it stays stubbornly prime even among the Gaussian integers — which is exactly why it can never be
coaxed into a sum of two squares. Brahmagupta's identity was a two-thousand-year-old hint of this
same splitting, dressed up in ordinary algebra.