Here is an interesting problem I have been playing around with. We all remember square numbers from grade school: 2^{2 }= 4 or 6^{2} = 36. Now consider the lowly number 5. While it can be squared, it, by itself, is not a square number. That makes 5 sad. However, maybe 5 could be the next best thing. Could we find a way to write 5 as the sum of two squares?

Well, we could try 5 = 2 + 3. But neither 2 or 3 is a square. What about 5 = 4 + 1? Bingo! 4 = 2^{2} and 1 = 1^{2}. The interesting thing, is that if you try to do this with 6, you find that is simply doesn’t work. It won’t work with 7 either, but it does with 8. Can you find the two squares that add up to 8?

Of course you can; 8 = 2^{2} + 2^{2}.

Now for the fun part. I guarantee you that it will work for 40. Why 40 you might ask? Well, if it worked for 5 and it worked for 8, then it will work for 5 × 8, or 40.

Sure enough, 40 = 36 + 4 = 6^{2} + 2^{2}.

Using this insight, we could go even higher. 40 × 8 = 320. Assuming I am correct, it should work for 320. Now I don’t know about you, but I have a hard time spotting two squares that add up to 320. Wouldn’t it be nice if there was a formula that would use what I already know about 8 and 40 to tell us which squares add up to 320?

As I was working through this problem, I stumbled upon just such a formula. It’s called the “sum of squares” because it sums two squares. The nice thing is that it tells us how to arrive at the two squares that we are going to add together.

Let’s first return to the case with 5 and 8. The base numbers that produced the squares for 5 were 2 and 1. Let’s call these a and b. For 8, the numbers are 2 and 2. Let’s call these c and d. I’m going to give you the formula and then explain at the end how we obtained it. For now, just trust me. At first glance, the formula may not feel intuitive. But if you hang in there and do the example yourself a couple of times, you’ll get comfortable with it pretty quickly.

First square = a × c + b × d

Second square = a × d – b × c

Not what you would expect hey? Those are some complicated formulas for such a simple puzzle. However, if we plug in our values, we get:

First square = 2 × 2 + 1 × 2 = 6

Second square = 2 × 2 – 1 × 2 = 2

Thus: 40 = 6^{2} + 2^{2}

Pretty cool hey! Let’s see if it works for 8 and 40:

8 = 2^{2} + 2^{2}

40 = 6^{2} + 2^{2}.

First square = 2 × 6 + 2 × 2 = 16

Second square = 2 × 2 – 2 × 6 = -8

So, we get: 320 = 16^{2} + (-8)^{2}

Ok, one more fun example.

13 = 2^{2} + 3^{2}

17 = 1^{2} + 4^{2}

First square = 2 × 1 + 3 × 4 = 14

Second square = 2 × 4 – 3 × 1 = 5

And we get: 221 = 14^{2} + 5^{2}

That’s all there is to it. A fun exercise is to find out which numbers between 1 and 100 can be written as the sum of two squares. For example, of the three numbers 71, 72, 73, only 2 can be written as the sum of two squares. Can you tell which ones?

Aside:

For those of you wondering why the formula works, a bit of algebra does the trick.

Let n_{1} and n_{2} be two positive integers where n_{1} = a^{2} + b^{2} and n_{2} = c^{2} + d^{2}. To show that the product, n_{1}n_{2}, can be written as the sum of two squares of integers, observe the following:

n_{1}n_{2} = (a^{2} + b^{2})(c^{2} + d^{2})

= a^{2}c^{2} + a^{2}d^{2} + b^{2}c^{2} + b^{2}d^{2}

= (a^{2}c^{2} + b^{2}d^{2}) + (a^{2}d^{2} + b^{2}c^{2})

= (a^{2}c^{2} + 2abcd + b^{2}d^{2}) + (a^{2}d^{2} – 2abcd + b^{2}c^{2})

=(ac + bd)^{2} + (ad – bc)^{2}