Number Theory

Which Numbers Are Sums of Two Squares?

Apply the prime factor test for writing a number as a² + b², see why it works, and count how many ways each number can be written.

Key takeaways

  • A number is a sum of two squares if and only if each prime that leaves remainder 3 modulo 4 occurs an even number of times.
  • No number of the form 4k + 3 is a sum of two squares, because squares leave only remainder 0 or 1 modulo 4.
  • Every prime of the form 4k + 1 is a sum of two squares in essentially one way.
  • A product of two sums of two squares is again a sum of two squares, by an explicit identity.

A positive integer is a sum of two squares exactly when, in its prime factorization, every prime of the form 4k + 3 appears to an even power. Here the squares are of integers, and zero is allowed, so 9 = 3² + 0² counts.

So 90 = 2 × 3² × 5 passes, and indeed 90 = 9² + 3². The number 21 = 3 × 7 fails, and no search will find two squares adding to it. The reasons behind this rule connect remainders, multiplication, a pigeonhole argument and the geometry of the plane.

Experiment first

Here are the numbers from 1 to 20 that can be written as a² + b²: 1, 2, 4, 5, 8, 9, 10, 13, 16, 17, 18 and 20. The missing ones are 3, 6, 7, 11, 12, 14, 15 and 19.

Look at the primes. The primes 2, 5, 13 and 17 are present. The primes 3, 7, 11 and 19 are absent. Apart from 2, the dividing line is the remainder on division by 4. That observation, made in the seventeenth century by Albert Girard and by Fermat, is the core of the theorem. Collecting data like this before guessing a rule is a habit worth copying, as described in using small cases to find patterns.

The easy obstruction

An even number squared is divisible by 4. An odd number squared leaves remainder 1. So a sum of two squares leaves remainder 0, 1 or 2 modulo 4, and never 3.

A stronger statement handles composite numbers. Suppose a prime p of the form 4k + 3 divides a² + b². If p did not divide b, you could divide by b and obtain a number whose square is −1 modulo p. But −1 is not a square modulo such a prime, as explained under quadratic residues. So p divides both a and b, and therefore p² divides a² + b². Divide it out and repeat. Each round removes two copies of p, so the total number of copies must be even.

Why does 2 behave differently?

The prime 2 equals 1² + 1², so it is representable, and it never causes trouble. Doubling a representable number keeps it representable in an explicit way:

2(a² + b²) = (a + b)² + (a − b)²

From 13 = 3² + 2² you get 26 = 5² + 1². Applied again, it gives 52 = 6² + 4², which is simply the original representation of 13 scaled by 2.

The reverse works as well. If an even number n is a sum of two squares, then the two numbers squared have the same parity, and halving their sum and difference represents n/2. So the factors of 2 can be ignored entirely when you apply the test. Only the odd primes decide.

The multiplication identity

(a² + b²)(c² + d²) = (ac − bd)² + (ad + bc)²
                   = (ac + bd)² + (ad − bc)²

Take 5 = 2² + 1² and 13 = 3² + 2². The first line gives 4² + 7² = 65, and the second gives 8² + 1² = 65.

This is the rule for multiplying the lengths of complex numbers: the quantity a² + b² is the squared length of a + bi, and lengths multiply. Because of the identity, to settle the theorem it is enough to represent the primes. Squares of the bad primes come along free, since multiplying a sum of two squares by q² just scales both terms by q.

Primes of the form 4k + 1

Fermat claimed that every such prime is a sum of two squares, and Euler published a proof in the eighteenth century. Here is a short one in the style of Axel Thue.

Since p ≡ 1 (mod 4), there is an s with s² ≡ −1 (mod p). Now consider all numbers a − sb with a and b between 0 and the integer part of √p. There are more than p such pairs, so by the pigeonhole principle two of them leave the same remainder. Subtracting those two gives x and y, not both zero, with

x ≡ s·y  (mod p),   |x| < √p,   |y| < √p
x² + y² ≡ s²y² + y² = y²(s² + 1) ≡ 0  (mod p)
0 < x² + y² < 2p

The only multiple of p strictly between 0 and 2p is p. So x² + y² = p.

For p = 13, take s = 5, since 25 ≡ −1. With y = 2 you get x ≡ 10 ≡ −3, and 9 + 4 = 13.

Don Zagier later published a famously brief proof based on pairing up solutions of x² + 4yz = p, refining an argument of Roger Heath-Brown.

Finding the representation in practice

For a prime p of the form 4k + 1 you can compute the two squares without searching all pairs.

  1. Find a number that is not a square modulo p, call it c, and set s = c^((p−1)/4). Then s² ≡ −1.
  2. Run the Euclidean algorithm on p and s.
  3. The first two remainders smaller than √p are the numbers you want.

Take p = 29. The number 2 is not a square modulo 29, and 2^7 = 128 ≡ 12. Check: 144 ≡ −1. The Euclidean algorithm gives 29 = 2 × 12 + 5, then 12 = 2 × 5 + 2, then 5 = 2 × 2 + 1. The square root of 29 is a little over 5, so the first two remainders below it are 5 and 2. And 25 + 4 = 29.

For small primes a table of squares is just as fast: subtract each square from p and see whether a square remains. For large primes the difference between the two methods is enormous.

Applying the test

n factorization verdict example
45 3² × 5 yes 6² + 3²
99 3² × 11 no 11 appears once
245 5 × 7² yes 14² + 7²
1001 7 × 11 × 13 no 7 and 11 appear once
2025 3⁴ × 5² yes 36² + 27²
2026 2 × 1013 yes 45² + 1²
Testing whether n is a sum of two squares
1 Factor n Write it as a product of prime powers 2 Pick out 4k + 3 List primes with remainder 3 modulo 4 3 Check exponents Each of those exponents must be even 4 Build the squares Combine prime representations with the identity Factor n Write it as a product of prime powers 1 Pick out 4k + 3 List primes with remainder 3 modulo 4 2 Check exponents Each of those exponents must be even 3 Build the squares Combine prime representations with the identity 4

Counting the ways

Let r(n) be the number of integer pairs (a, b) with a² + b² = n, counting signs and order separately. A classical formula says

r(n) = 4 × (d₁(n) − d₃(n))
d₁ = number of divisors of the form 4k + 1
d₃ = number of divisors of the form 4k + 3

For n = 25, the divisors 1, 5 and 25 are all of the first kind, so r(25) = 12. The pairs are (±5, 0), (0, ±5), (±3, ±4) and (±4, ±3). For n = 65 there are four such divisors, so r(65) = 16, matching the two essentially different representations found above.

For a prime p ≡ 1 (mod 4), r(p) = 8, which means one representation up to order and sign.

Averages and density

Counting lattice points inside a circle of radius √N shows that r(n), averaged over n up to N, approaches π. The area of the circle is πN, and each lattice point inside corresponds to one representation of some number.

Yet most numbers have no representation at all. Edmund Landau, and independently Ramanujan, found that the count of representable numbers up to N grows like a constant (about 0.764) times N divided by √(ln N), where ln is the natural logarithm. The proportion therefore tends to zero, though very slowly. The average stays near π because the representable numbers tend to have many representations.

A larger view

The theorem describes how ordinary primes behave among the Gaussian integers, numbers a + bi with a and b integers. A prime of the form 4k + 1 splits, as in 5 = (2 + i)(2 − i). A prime of the form 4k + 3 stays prime. The prime 2 is special. Factorization there is unique, and that fact gives another proof of the whole theorem, a first taste of algebraic number theory.

Ask for three squares and the answer changes: only numbers of the form 4^a(8b + 7) are excluded. With four, every positive integer works, as shown in Lagrange's theorem.

Where you will meet this

The theorem answers geometric questions. The distances possible between two points of a square grid are exactly the square roots of numbers that pass the test. No two grid points are a distance √21 apart. A circle centered at the origin passes through grid points only when the square of its radius is representable, and it passes through many of them when that square has many prime factors of the form 4k + 1.

It also tells you which integers can be the hypotenuse of a right triangle with whole-number sides. The hypotenuse must be divisible by at least one prime of the form 4k + 1, as in 5, 10, 13, 15 and 17. Numbers such as 7, 9 or 21 never appear as the longest side. The construction of the triangles themselves is covered in Pythagorean triples.

Two worked decisions

Is 3,060 a sum of two squares? Factor it: 3060 = 4 × 9 × 5 × 17. The only prime of the form 4k + 3 is 3, and it appears twice, so the answer is yes. Set the factor 36 = 6² aside and represent 5 × 17 = 85 = 9² + 2². Multiplying both terms by 6 gives 54² + 12² = 2916 + 144 = 3060.

Is 1,518 a sum of two squares? Here 1518 = 2 × 3 × 11 × 23. The primes 3, 11 and 23 each appear once, so the answer is no, and you are spared checking some thirty-eight possible squares.

Common mistakes

  • Requiring every prime factor to be of the form 4k + 1. Primes of the form 4k + 3 are fine in pairs, and 2 is always fine.
  • Concluding from n ≡ 1 (mod 4) that n works. The number 21 is a counterexample.
  • Forgetting zero. The number 49 is 7² + 0², and has no other representation.
  • Counting (1, 2) and (2, 1) as different when a problem asks for sets of squares, or as the same when it asks for r(n).

Written for 4Mathematics with AI writing tools and not reviewed line by line by a person. Check anything you rely on against a textbook or primary source. Report an error.

Keep reading

All 59 articles in Number Theory →

Latest news on this topic