14 November 2013

This has nothing to do with the playground game, the church, or the mobile/social/local city guide that helps you make the most of where you are. (Disclosure: I work at Foursquare.)

This is about Lagrange’s four-square theorem, which states that every natural number can be expressed as the sum of four squares. For example,

The proof given on the Wikipedia page is only an existence proof, but I was able to find a mostly constructive proof elsewhere online. I want to present an outline of the proof along with some code that carries out the construction. Here’s a preview:

*Main> foursquare 123456789
(-2142,8673,6264,-2100)


### Outline of the constructive proof

To write a number $n$ as the sum of four squares:

1. If $n$ is composite, find its prime factors and write each of the factors as the sum of four squares. Then combine them pairwise using Euler’s four-square identity, which shows that the product of two numbers that are the sum of four squares can itself be written as the sum of four squares.

2. For each prime factor $p$, find numbers $a$, $b$, $c$, and $d$ such that $a^2 + b^2 + c^2 + d^2 \equiv 0 \m p$. There are 3 cases here:

a. If $p = 2$, then $a = b = 1$ and $c = d = 0$.

b. If $p \equiv 1 \m 4$, find $a$ such that $a^2 + 1^2 \equiv 0 \m p$, letting $b = 1$ and $c = d = 0$.

c. If $p \equiv 3 \m 4$, find $a$ and $b$ such that $a^2 + b^2 + 1^2 \equiv 0 \m p$, letting $c = 1$ and $d = 0$.

3. Now that we have $a^2 + b^2 + c^2 + d^2 = kp$, construct $a'$, $b'$, $c'$, and $d'$ such that $a'^2 + b'^2 + c'^2 + d'^2 = mp$ where $m \lt k$. Repeat this procedure until $a'^2 + b'^2 + c'^2 + d'^2 = p$.

The whole flow of the program looks like this:

So we factor n, and for each prime factor, find its four-square decomposition using foursquare', then combine them pairwise. For its part, foursquare' does some magic to find initial values for our 4 numbers and shuffles them off to reduce to get the sum of their squares to be exactly equal to p instead of a multiple of p.

I also defined the operator |> as backwards function application. It makes more sense to me to push a value through a series of functions than to have to read all my expressions inside out.

Alright, now I’m going to go through piece by piece and fill in missing definitions.

### The easy parts

Factoring is probably the second Haskell program you ever wrote (the first probably also beginning with “factor”). Here it is from scratch, for fun:

And yes, there are more efficient ways to generate primes.

combine is a straighforward realization of Euler’s four-square identity:

I’m also going to need modular exponentiation, to compute $a^n \m p$:

And a helper for finding the sum of squares of 4 numbers:

Before we go on, let’s check that combine works.

*Main> let x = (1, 2, 3, 4)
*Main> let y = (5, 6, 7, 8)
*Main> sumOfSquares x
30
*Main> sumOfSquares y
174
*Main> sumOfSquares (combine x y)
5220
*Main> 30*174
5220


Let’s do that 100 more times.

*Main> quickCheck (\x y -> (sumOfSquares x) * (sumOfSquares y) == sumOfSquares(combine x y))
+++ OK, passed 100 tests.


I freaking love quickCheck.

### Odd primes congruent to 1 mod 4

Now to fill in the missing parts of foursquare'. Here’s the first clause:

Here we have to find $a$ such that $a^2 + 1 \equiv 0 \m p$. In other words, we’re looking for a square root of $-1 \m p$. By Fermat’s little theorem, if $n \lt p$ then $n^{p-1} \equiv 1 \m p$, which means that its square root, $n^{(p-1)/2}$, is congruent to $\pm 1 \m p$. If we can find one $n$ such that $n^{(p-1)/2} \equiv -1 \m p$, then its square root, $n^{(p-1)/4}$, is the $a$ we’re after. Notice this only works because $(p-1)/4$ is an integer.

Well luckily it turns out that half of the time, $n^{(p-1)/2} \equiv -1 \m p$, so if we just try numbers sequentially we’re liable to find one pretty quickly. It also turns out that the smallest such $n$ is always a prime number, so we can narrow down the search that way.

The code is below. We’re just trying prime numbers $n$ until one of them satisifies $(n^{(p-1)/4})^2 \equiv -1 \m p$.

### Odd primes congruent to 3 mod 4

Here’s the next clause of foursquare':

This case is a little more difficult. We’re looking for $a$ and $b$ such that $a^2 + b^2 + 1 \equiv 0 \m p$. Equivalently, we’re looking for a $b$ that is a square root of $-1 - a^2 \m p$. We’re guaranteed that there is some $a$ such that $-1 - a^2$ has a square root $\m p$, but I’ll refer you to the the full proof for the details on why this is. For our purposes, all we need to do is try different values of $a$ sequentially and check whether they produce a square root. That’s easy to check, at least: if $p \equiv 1 \m 4$, then $x^{(p+1)/4}$ is a square root of $x \m p$, if it has one. Here’s a quick proof:

since $(x^{\frac{1}{2}})^{p-1} \equiv 1 \m p$ by Fermat’s little theorem. (This doesn’t always produce a square root of $x \m p$ since it might instead be a square root of $-x \m p$.)

The proof gets a little hand-wavy on this part, but it turns out that finding an $a$ such that $-1 - a^2$ has a square root mod $p$ is pretty easy. It points out that -2 has a square root mod $p$ for all $p \equiv 3 \m 8$, which is already half of the cases.

Anyway, here’s the code:

### The reduce phase

Now that we have $a^2 + b^2 + c^2 + d^2 = kp$, we want to somehow construct a new set of numbers $a'$, $b'$, $c'$, and $d'$ such that $a'^2 + b'^2 + c'^2 + d'^2 = mp$ where $m \lt k$. Then we can repeat the procedure until $a'^2 + b'^2 + c'^2 + d'^2 = p$.

If $k$ is even, then $a^2 + b^2 + c^2 + d^2$ is even, meaning 0, 2 or all 4 of the numbers are even. Rearranging them according to their remainder mod 2, we can get it so that $a \pm b$ and $c \pm d$ are both even. Then we can divide through by 2 and get

and we have the reduction we need. In code:

making use of the convenience functions

If $k$ is odd, we can replace each of the 4 numbers by their “absolute least residue” mod $k$. All this means is, if $a \gt \frac{k}{2}$, we replace it with $a - k$, which reduces its absolute value without changing its meaning mod $k$. So let $a'$ be the absolute least residue of $a \m k$ and so on. Since all of them are smaller than $\frac{k}{2}$, their squares are less than $\frac{k^2}{4}$ and the sum of their squares is less than $k^2$. The sum of their squares is still $0 \m k$, so the sum must be equal to $mk$ for some $m \lt k$.

Now we can use combine to construct the product

The combine function is written in such a way that each of the terms $w$, $x$, $y$, and $z$ will be divisible by $k$. This is easy to see by inspecting each term and remembering that $a \equiv a' \m k$, etc. So we can divide out $k$ from each of the terms to get a product equal to $mp$, and we have the reduction we need.

Here’s the complete code for reduce:

And that completes the construction.

Let’s try it out:

*Main> foursquare 2013
(10,12,40,13)
*Main> foursquare 20131114
(4455,533,0,0)
*Main> foursquare 1729
(32,-22,-11,10)
*Main> quickCheck (\n -> n >= 0 ==> sumOfSquares (foursquare n) == n)
+++ OK, passed 100 tests.


Fun times.

So yeah, this is pretty useless, but it was fun to see it actually work. The code is available in this gist if you want to play around with it.