Four square theorem

The 'Four square theorem states that every positive integer can be expressed as the sum of four squares. (Some of the squares may be zero.)

Proof outline
Let n be a positive integer. We'll prove that
 * n=a2+b2+c2+d2, for integers a, b, c, d.

We see that 1 can be expressed as the sum of four squares, namely 1+0+0+0, and 2 can be expressed as 1+1+0+0. We shall see that primes of the form 4k+1 can be expressed as the sum of two squares, i.e. a2+b2+0+0. And we shall see that all primes of the form 4k+3 can be expressed as the sum of four squares. Finally, we know from the Quaternion identity that the product of two numbers, each of which can be expressed as the sum of four squares, can be expressed as the sum of four squares. By repeated application of the Quaternion identity, we have a way to express as the sum of four squares any nonempty finite product of numbers, each expressed as the sum of four squares. Since n can be decomposed as the product of 1 and a bunch of primes, n can be expressed as the sum of four squares.

Constructive proof
The proof presented here will be constructive. MathHelp has taken up the task of writing algorithms, which can be done by hand, or written as VBA programs that actually find the four squares whose sum is any given number, n. To be as helpful as possible, we will make some observations that simplify the task. We'll follow that by the identities and lemmas we'll need for the proof itself.

Some observations
If n has a square factor, that is, if the prime factorization of n includes any primes raised to an exponent of 2 or larger, then all of these even-exponent primes can be gathered together to find the largest square factor of n, which we can then divide out. Let s2 be this large square factor, with n=ms2, and so m is squarefree. Then
 * m=a2+b2+c2+d2 implies n=(sa)2+(sb)2+(sc)2+(sd)2.

If n is even, n=2m, and
 * m=a2+b2+c2+d2 implies

n=2m=(a+b)2+(a-b)2+(c+d)2+(c-d)2

WLOG, for our proof, we'll assume n is squarefree and odd. For the algorithm to actually find the four squares, we'll start by factoring out the largest square, and any remaining factor of two, and hold those factors aside until the end.