A natural number can be written can sum of three squares,
if and only if
is not of the form
for nonnegative integers
and
Modulo every square has to be
,
or
and hence
cannot be
Also if
divides
then
all have to be even. These two facts prove the only if part.
Now to show the existence of solutions for which satisfies these constraints- we use reduction of ternary quadratic forms, and the fact that
is the the unique quasi-reduced form of discriminant
. Given these facts, if we can create some ternary quadratic form of the same discriminant which represents
, by changing coordinates, we can reduce it to
(by the uniqueness) and hence found solutions of
Let be the adjoint of
A positive-definite ternary quadratic form is called quasi-reduced if
(i)
(ii)
(iii)
(iv)
(v)
Use this conditions we can verify that is the unique positive definite quasi reduced form of discriminant
Now given any satisfying the local conditions, we consider the form
which represents
because
To make sure that it is positive definite we need
Discriminant one condition is
, so these conditions are equivalent to finding
such
being a square modulo
Now the problem is about finding such a for any
satisfying the local conditions and this can be done by quadratic reciprocity as follows:
Choose a prime so that
Now
Similarly for choose
If , choose
If
or
, choose
With these choices in all the cases we can see that
Thus we are done. To recap, we used congruence conditions, quadratic reciprocity to find primes of the form
such that
is a square mod
Once we have such a
, we can construct a positive definite form of discriminant
which represents
(by construction, it has a special form). And now by reduction theory and the uniqueness of quasi reduced forms of discriminant
, this form has to be equivalent to
Thus we found solutions to