Which Integers Are Sums of Two, Three, or Four Squares?

Can every positive integer be written as a sum of squares? The answer depends on how many squares we allow. With two squares, prime numbers impose a surprisingly restrictive condition. With three squares, a simple pattern involving powers of 4 and residues modulo 8 determines which integers are impossible. With four squares, something remarkable happens: every nonnegative integer is a sum of four squares.

These three questions lead to some of the most beautiful classical theorems in number theory.

1. Sums of Two Squares

Let us begin with a familiar question: which positive integers can be represented in the form

n=a^2+b^2

For example,

25=3^2+4^2,\qquad 65=1^2+8^2,\qquad 50=1^2+7^2.

But not every integer has such a representation. For instance, 3, 6, 7, 11, and 21 cannot be expressed as sums of two integer squares.

The role of prime numbers

The key is to examine prime numbers modulo 4.

Every prime number other than 2 is congruent to either 1 or 3 modulo 4. These two types behave differently when we try to express an integer as a sum of two squares.

Consider a prime number p congruent to 3 modulo 4. If

p\mid (a^2+b^2),

then necessarily

p\mid a\quad\text{and}\quad p\mid b.

Indeed, if b were not divisible by p, then the congruence would imply that -1 is a quadratic residue modulo p. But -1 is not a quadratic residue when p is congruent to 3 modulo 4.

This observation explains why primes of this type must occur in pairs in a sum-of-two-squares representation.

Fermat’s two-square theorem

Theorem. A positive integer n can be expressed as a sum of two integer squares if and only if every prime factor congruent to 3 modulo 4 occurs with an even exponent in the prime factorization of n.

For example, consider

45=3^2\cdot 5.

The prime 3 occurs with an even exponent, so the theorem guarantees a representation. Indeed,

45=3^2+6^2.

In contrast,

21=3\cdot 7

contains two primes congruent to 3 modulo 4, each with an odd exponent. Therefore, 21 cannot be a sum of two squares.

The theorem gives a complete answer, even when finding the actual representation requires additional work.

2. Sums of Three Squares

What changes if we allow three squares?

n=a^2+b^2+c^2.

Many integers that cannot be represented by two squares become representable. For example,

14=3^2+2^2+1^2.

However, there is still an obstruction. The surprising fact is that it can be described using only powers of 4 and remainders modulo 8.

Why does the number 7 appear?

Every integer square is congruent to 0, 1, or 4 modulo 8:

x^2\equiv 0,1,\text{ or }4\pmod 8.

Consequently, the sum of three squares can never be congruent to 7 modulo 8.

For example, 7 cannot be expressed as a sum of three squares.

There is a second part to the obstruction. If a sum of three squares is divisible by 4, then all three numbers must be even. To see this, observe that an odd square is congruent to 1 modulo 4. Since each square is congruent to either 0 or 1 modulo 4, a sum of three squares can be divisible by 4 only when all three squares are divisible by 4.

Thus, if a number is representable by three squares and divisible by 4, we can divide the entire representation by 4 and obtain another representation.

Repeating this process explains why the obstruction is not limited to numbers congruent to 7 modulo 8. It also includes numbers that become congruent to 7 after removing a power of 4.

Legendre’s three-square theorem

Theorem. A positive integer n can be expressed as a sum of three integer squares if and only if it is not of the form

n=4^a(8b+7),\qquad a,b\geq 0.

Let us look at a few examples.

  • 7: It has the forbidden form with a = 0 and b = 0.
  • 28: We have 28 = 4 × 7, so it is also forbidden.
  • 14: It is not of the forbidden form, and indeed 14 = 3² + 2² + 1².
  • 30: It is not forbidden, and 30 = 5² + 2² + 1².

Notice something interesting: 28 cannot be represented by three squares, but it can be represented by four:

28=4^2+2^2+2^2+2^2.

This brings us to the final theorem.

3. Every Integer Is a Sum of Four Squares

We have seen that two squares impose strong restrictions, and three squares still leave an infinite family of exceptions.

With four squares, all these restrictions disappear.

Lagrange’s four-square theorem

Theorem. Every nonnegative integer can be expressed as a sum of four integer squares.

In other words, for every nonnegative integer n, there exist integers a, b, c, and d such that

n=a^2+b^2+c^2+d^2.

For example,

7=2^2+1^2+1^2+1^2, 15=3^2+2^2+1^2+1^2, 28=4^2+2^2+2^2+2^2.

The theorem was proved by Joseph-Louis Lagrange in 1770. Its proof is substantially deeper than the elementary modular arguments we used for the three-square obstruction.

We will not reproduce the full proof here. The important point is the remarkable contrast: three squares leave infinitely many exceptions, whereas four squares suffice for every nonnegative integer.

4. Comparing the Three Theorems

We can summarize the results as follows.

Number of squares Characterization
Two Every prime congruent to 3 modulo 4 must have an even exponent.
Three The integer must not have the form 4a(8b+7).
Four Every nonnegative integer is representable.

There is a beautiful progression here. The first theorem is controlled by prime factorization, the second by congruences modulo 8 and powers of 4, and the third guarantees that no arithmetic obstruction remains.

5. Try It Yourself

Here are four problems to test your understanding. Open each solution after trying the problem.

Problem 1. Can 77 be expressed as a sum of two squares?

Solution. We have

77=7\cdot 11.

Both prime factors are congruent to 3 modulo 4 and occur to odd exponents. Therefore, 77 cannot be expressed as a sum of two squares.

Problem 2. Can 100 be expressed as a sum of two squares? Find a representation.

Solution. Yes. In fact,

100=10^2+0^2.

The prime factorization is 100 = 2² × 5², so the two-square theorem also guarantees representability.

Problem 3. Which of the following cannot be expressed as a sum of three squares: 19, 28, 44, 60?

Solution. Two numbers are impossible: 28 and 60. Indeed,

28=4(7),\qquad 60=4(8+7).

Both have the forbidden form. The other two have representations:

19=4^2+1^2+1^2, 44=6^2+2^2+2^2.
Problem 4. Explain why 112 cannot be expressed as a sum of three squares, but can be expressed as a sum of four squares.

Solution. Since

112=16\cdot 7=4^2(8\cdot 0+7),

the three-square theorem rules out a representation by three squares.

However, Lagrange’s theorem guarantees four squares. One representation is

112=8^2+4^2+4^2+4^2.

6. A Final Perspective

The three theorems illustrate an important theme in number theory: adding just one more square can fundamentally change a representation problem.

There is also a deeper connection to algebra. The identities for sums of two, four, and eight squares are related to multiplication in the complex numbers, quaternions, and octonions. These exceptional dimensions are explored in our companion article:

Why Are 1, 2, 4, and 8 Special in Sums of Squares?

The results in this article are a starting point for further exploration. A natural next question is not only whether an integer can be represented as a sum of squares, but how many different representations it has.

Continue exploring: To see how two sums of four squares multiply into a third, read How Euler’s Four-Square Identity Generates New Sums of Four Squares. For sums of two squares through complex numbers, see Arctangent Identities, Gaussian Integers, and Number Theory.

References

  1. O. Taussky, “Sums of Squares,” The American Mathematical Monthly, Vol. 77, No. 8 (1970), pp. 805–830. JSTOR article. See especially Section 3, “Sums of squares in number theory.”
  2. J.-L. Lagrange, “Démonstration d’un théorème d’arithmétique,” Nouveaux Mémoires de l’Académie Royale des Sciences et Belles-Lettres de Berlin (1770; published 1772), pp. 123–133.

Leave a Reply

Your email address will not be published. Required fields are marked *