Lecture 21: Binary Quadratic Forms I: Sums of Two Squares

William Stein Math 124 Harvard University Fall 2001

Today we study the question of which integers are the sum of two squares.

1 Sums of Two Squares

During the next four lectures, we will study binary quadratic forms. A simple example of a binary quadratic form that will occupy us today is

x^{2} + y^{2}.

A typical question that one asks about a quadratic form is which integers does it represent. “Are there integers x and y so that x^{2}+y^{2} = 389? So that x^{2}+y^{2} = 2001?”

1.1 Which Numbers are the Sum of Two Squares?

The main goal of today’s lecture is to prove the following theorem. Theorem 1.1. A number n is a sum of two squares if and only if all prime factors of n of the form 4m + 3 have even exponent in the prime factorization of n.

Before tackling a proof, we consider a few examples. Example 1.2.

• 5 = 1^{2} + 2^{2}.

• 7 is not a sum of two squares.

• 2001 is divisible by 3 because 2 + 1 is, but not by 9 since 2 + 1 is not, so 2001 is not a sum of two squares.

• 2 · 3^{4} · 5 · 7^{2} · 13 is a sum of two squares.

• 389 is a sum of two squares, since 389 ≡ 1 (mod 4) and 389 is prime.

• 21 = 3 · 7 is not a sum of two squares even though 21 ≡ 1 (mod 4).

In preparation for the proof of Theorem 1.1, we recall a result that emerged when we analyzed how partial convergents of a continued fraction converge.

Lemma 1.3. If x ∈ ℝ and n ∈ ℕ, then there is a fraction \frac{a}{b} in lowest terms such that 0 < b ≤ n and

|x − \frac{a}{b}| ≤ \frac{1}{b(n + 1)}.

Proof. Let [a_{0}, a_{1}, …] be the continued fraction expansion of x. As we saw in the proof of Theorem 2.3 in Lecture 18, for each m

|x − \frac{p_{m}}{q_{m}} | < \frac{1}{q_{m} · q_{m+1}} .

Since q_{m+1} is always at least 1 bigger than q_{m} and q_{0} = 1, either there exists an m such that q_{m} ≤ n < q_{m+1}, or the continued fraction expansion of x is finite and n is larger than the denominator of the rational number x. In the first case,

|x − \frac{p_{m}}{q_{m}} | < \frac{1}{q_{m} · q_{m+1}} ≤ \frac{1}{q_{m} · (n + 1)},

so \frac{a}{b} = \frac{p_{m}}{q_{m}} satisfies the conclusion of the lemma. In the second case, just let \frac{a}{b} = x.

Definition 1.4. A representation n = x^{2} + y^{2} is primitive if gcd(x, y) = 1.

Lemma 1.5. If n is divisible by a prime p of the form 4m + 3, then n has no primitive representations.

Proof. If n has a primitive representation, n = x^{2} + y^{2}, then

p | x^{2} + y^{2} and gcd(x, y) = 1,

so p ∤ x and p ∤ y. Thus x^{2} + y^{2} ≡ 0 (mod p) so, since ℤ/pℤ is a field we can divide by y^{2} and see that

(x/y)^{2} ≡ −1 (mod p).

Thus the quadratic residue symbol (\frac{−1}{p}) equals +1. However,

(\frac{−1}{p}) = (−1)^{\frac{p−1}{2}} = (−1)^{\frac{4m+3−1}{2}} = (−1)^{2m+1} = −1.

Proof of Theorem 1.1. (⟹) Suppose that p is of the form 4m + 3, that p^{r} || n (exactly divides) with r odd, and that n = x^{2} + y^{2}. Letting d = gcd(x, y), we have

x = dx′, y = dy′, n = d^{2}n′

with gcd(x′, y′) = 1 and

(x′)^{2} + (y′)^{2} = n′.

Because r is odd, p | n′, so Lemma 1.5 implies that gcd(x′, y′) > 1, a contradiction.

(⟸) Write n = n_{1}^{2}n_{2} where n_{2} has no prime factors of the form 4m + 3. It suffices to show that n_{2} is a sum of two squares. Also note that

(x_{1}^{2} + y_{1}^{2})(x_{2}^{2} + y_{2}^{2}) = (x_{1}x_{2} + y_{1}y_{2})^{2} + (x_{1}y_{2} − x_{2}y_{1})^{2},

so a product of two numbers that are sums of two squares is also a sum of two squares.^{1} Also, the prime 2 is a sum of two squares. It thus suffices to show that if p is a prime of the form 4m + 1, then p is a sum of two squares.

Since

(−1)^{\frac{p−1}{2}} = (−1)^{\frac{4m+1−1}{2}} = +1,

−1 is a square modulo p; i.e., there exists r such that r^{2} ≡ −1 (mod p). Taking n = ⌊\sqrt{p}⌋ in Lemma 1.3 we see that there are integers a, b such that 0 < b < \sqrt{p} and

|−\frac{r}{p} − \frac{a}{b}| ≤ \frac{1}{b(n + 1)} < \frac{1}{b\sqrt{p}}.

If we write

c = rb + pa

then

|c| < \frac{pb}{b\sqrt{p}} = \frac{p}{\sqrt{p}} = \sqrt{p}

and

0 < b^{2} + c^{2} < 2p.

But c ≡ rb (mod p), so

b^{2} + c^{2} ≡ b^{2} + r^{2}b^{2} ≡ b^{2}(1 + r^{2}) ≡ 0 (mod p).

Thus b^{2} + c^{2} = p.

1.2 Computing x and y

Suppose p is a prime of the form 4m + 1. There is a construction of Legendre of x and y that is explained on pages 120–121 of Davenport. I’m unconvinced that it is any more efficient than the following naive algorithm: compute \sqrt{p − x^{2}} for x = 1, 2, … until it’s an integer. This takes at most \sqrt{p} steps. Here’s a simple PARI program which implements this algorithm.

1This algebraic identity is secretely the assertion that the norm map N : ℚ(i)^{∗} → ℚ^{∗} sending x + iy to (x + iy)(x − iy) = x^{2} + y^{2} is a homomorphism.

{sumoftwosquares(n) =
   local(y);
   for(x=1,floor(sqrt(n)),
      y=sqrt(n-x^2);
      if(y-floor(y)==0, return([x,floor(y)]))
   );
   error(n," is not a sum of two squares.")
}

2 Sums of More Squares

Every natural number is a sum of four squares. See pages 124–126 of Davenport for a proof.

A natural number is a sum of three squares if and only if it is not a power of 4 times a number that is congruent to 7 modulo 8. For example, 7 is not a sum of three squares. This is more difficult to prove.