On this page
Take any odd prime and divide it by 4. The remainder is either 1 or 3, and that one remainder tells you whether the prime can be written as a² + b² with whole numbers a and b. This is Fermat's theorem on sums of two squares, and you can test it yourself in a few minutes with a pencil.
Which primes are a sum of two squares?
An odd prime p is a sum of two squares if and only if p leaves remainder 1 when divided by 4, as set out in the Wikipedia article on Fermat's theorem on sums of two squares. The only even prime, 2, also works, because 2 = 1² + 1².
Here are the three cases from our title:
5 = 1² + 2²
13 = 2² + 3²
29 = 2² + 5²
Primes of the form 4k + 1 are called Pythagorean primes, and OEIS sequence A002144 lists them: 5, 13, 17, 29, 37, 41, 53, 61, 73, 89, 97 and onwards. Every one of them splits into two squares.
Albert Girard was the first to make the observation, in work published in 1625. Fermat stated his own version in a letter to Marin Mersenne dated 25 December 1640, which is why it is sometimes called Fermat's Christmas theorem, according to the same Wikipedia article. He gave no proof. Euler announced one in letters to Goldbach in 1747 and 1749 and published the details in two papers between 1752 and 1755. In 1990 Don Zagier published a one-sentence proof that every prime p ≡ 1 (mod 4) is a sum of two squares in The American Mathematical Monthly.
Why can 3, 7 and 11 never be a sum of two squares?
Every whole number is even or odd, so look at what its square leaves on division by 4:
An even number is 2m, and (2m)² = 4m², so the remainder is 0.
An odd number is 2m + 1, and (2m + 1)² = 4m² + 4m + 1, so the remainder is 1.
So every square leaves remainder 0 or 1 on division by 4. A sum of two squares can therefore leave 0 + 0, 0 + 1 or 1 + 1, that is 0, 1 or 2, and never 3. Since 3, 7 and 11 each leave remainder 3, no choice of a and b can reach them. You can confirm 11 by brute force: the squares below 11 are 0, 1, 4 and 9, and no two of them add up to 11.
If the even-or-odd split looks familiar, it is the same proof by cases we use in how to prove n² + n is always even.
How do you find the two squares for a 4k + 1 prime?
For primes you meet in school or olympiad practice, a simple search on paper is enough:
Check the remainder: p divided by 4 must leave 1.
Try a = 1, 2, 3 and so on, while a² is no more than half of p.
For each a, work out p − a² and ask whether it is a perfect square.
Stop at the first yes and write p = a² + b².
Worked example, p = 97, which is 4 × 24 + 1: 97 − 1 = 96, 97 − 4 = 93, 97 − 9 = 88, 97 − 16 = 81 = 9². So 97 = 4² + 9², the same pair that the Euclidean-algorithm method gives in the Wikipedia article's worked example.
If you get stuck turning the remainder argument into a written proof, the Fermi Problem Library has a step-by-step solver that explains the reasoning rather than printing an answer.
Which whole numbers are a sum of two squares?
The full rule covers every whole number. A number n is a sum of two squares exactly when every prime factor of n of the form 4k + 3 appears an even number of times in its prime factorisation, as noted in OEIS sequence A001481, which lists these numbers: 0, 1, 2, 4, 5, 8, 9, 10, 13, 16, 17, 18, 20, 25 and onwards.
The tool behind the rule is the identity (a² + b²)(c² + d²) = (ac + bd)² + (ad − bc)², credited to Diophantus in the Wikipedia article: multiply two sums of two squares and you get another sum of two squares.
Try it on three numbers:
65 = 5 × 13. With 5 = 1² + 2² and 13 = 2² + 3², the identity gives (1·2 + 2·3)² + (1·3 − 2·2)² = 8² + 1² = 64 + 1.
45 = 3² × 5. The 3 appears twice, so 45 qualifies: 45 = 3² + 6².
21 = 3 × 7. Both primes are of the form 4k + 3 and each appears once, so 21 is not a sum of two squares.
How should you practise this?
A three-step plan for one evening:
Write the remainder-mod-4 proof from memory.
Sort the primes below 100 by remainder on division by 4, and split every 4k + 1 prime into two squares.
Factorise five composite numbers, predict which are sums of two squares, then check by search.
For the background ideas (primes, remainders, factorisation), the Fermi Concept Library covers concepts across mathematics, physics, chemistry, biology and computing. If you want to know why the factorisation step depends on primes being defined carefully, read why 1 is not a prime number, and for how common primes are overall, see how many primes lie below a number.


