Number TheoryDiophantine Equations and Continued Fractions

Sums of Two Squares

n is a sum of two squares exactly when every prime ≡ 3 (mod 4) appears in its factorization to an even power.

Not started Jump to practice

$5 = 1^{2} + 2^{2}$ and $13 = 2^{2} + 3^{2}$, but no matter how you try, $7$ and $21$ are not sums of two squares. Fermat found the pattern, and the supplementary law for $-1$ is the key to it.

Theorem (Fermat; proved by Euler). An odd prime $p$ is a sum of two squares exactly when $p \equiv 1 \pmod{4}$. More generally, a positive integer $n$ is a sum of two squares of integers exactly when every prime $q \equiv 3 \pmod{4}$ appears in the prime factorization of $n$ to an even power.

Why it works

Why $3 \bmod 4$ primes fail. Squares are $0$ or $1 \bmod 4$, so a sum of two squares is $0$, $1$ or $2 \bmod 4$, never $3$.
Products of sums are sums. $(a^{2} + b^{2})(c^{2} + d^{2}) = (ac - bd)^{2} + (ad + bc)^{2}$, an identity you can check by expanding.
Primes $\equiv 1 \bmod 4$ succeed. Here $-1$ is a square mod $p$: some $x$ has $p \mid x^{2} + 1$, a sum of two squares divisible by $p$. A short argument (Thue's lemma, or Fermat's "descent") shrinks this to $p$ itself.
Why even powers. If $q \equiv 3 \pmod{4}$ divides $a^{2} + b^{2}$, then $q$ divides both $a$ and $b$ (otherwise $-1 \equiv (a b^{-1})^{2}$ would be a square mod $q$). So $q^{2}$ divides the sum, and dividing it out leaves a smaller sum of two squares.

Worked example: Primes

Write $13$ and $29$ as sums of two squares, and explain why $23$ cannot be one.
  1. $13 \equiv 1$ and $29 \equiv 1 \pmod{4}$. Search: $13 - 2^{2} = 9 = 3^{2}$, and $29 - 2^{2} = 25 = 5^{2}$.
  2. $23 \equiv 3 \pmod{4}$, and a sum of two squares is never $3 \bmod 4$.

Answer: $13 = 3^{2} + 2^{2}$, $29 = 5^{2} + 2^{2}$; $23$ is impossible.

Worked example: A prime 3 mod 4, squared

Is $45$ a sum of two squares?
  1. $45 = 3^{2} \cdot 5$: the prime $3 \equiv 3 \pmod{4}$ appears to an even power, so yes.
  2. Take $5 = 2^{2} + 1^{2}$ and multiply by $9 = 3^{2}$: $45 = 6^{2} + 3^{2}$.

Answer: Yes: $45 = 36 + 9$.

Worked example: Multiplying representations

Write $65 = 5 \cdot 13$ as a sum of two squares in two ways.
  1. $5 = 2^{2} + 1^{2}$ and $13 = 3^{2} + 2^{2}$.
  2. $(ac - bd)^{2} + (ad + bc)^{2}$ with $(a, b, c, d) = (2, 1, 3, 2)$: $(6 - 2)^{2} + (4 + 3)^{2} = 16 + 49$.
  3. Swapping the roles of $c$ and $d$ gives $(4 - 3)^{2} + (6 + 2)^{2} = 1 + 64$.

Answer: $65 = 4^{2} + 7^{2} = 1^{2} + 8^{2}$.

Common mistake

Ruling out every number with a prime factor that is $3 \bmod 4$. $45 = 3^{2} \cdot 5$ has the factor 3, yet $45 = 6^{2} + 3^{2}$: the 3 appears squared. Only an odd power of such a prime is fatal, as in $21 = 3 \cdot 7$ or $63 = 3^{2} \cdot 7$. Also remember that $0$ counts: $49 = 7^{2} + 0^{2}$.

Key idea

$n$ is a sum of two squares exactly when each prime $\equiv 3 \pmod{4}$ divides $n$ to an even power. Primes $\equiv 1 \pmod{4}$ are sums of two squares, and products of sums are sums.

Practice

Pass with 3 right answers, the last 2 in a row. Each right answer earns 3 XP.

Loading practice…

Builds on

Leads to

This is one of the top rungs of Number Theory.

Sources and further reading

  • William Stein, Elementary Number Theory: Primes, Congruences, and Secrets (Springer, 2009; free online edition), chapter 5, Continued Fractions (sums of two squares)
  • MIT OpenCourseWare 18.781 Theory of Numbers (Spring 2012), lecture notes on sums of two squares
@teddyrookbook GitHub