TITLE: sum of squares in ring of integers
QUESTION [17 upvotes]: Lagrange proved that every positive integer is a sum of 4 squares. 
Are there general results like this for rings of integers of number fields? Is this class field theory? 
Explicitly, suppose a number field is formally real. Denote its ring of integers by $Z$. Is it true that for every algebraic integer $x$ in $Z$ either $x$ or $-x$ is a sum of squares?

REPLY [50 votes]: To address the particularities of this question for number fields, the basic theorem is attributed to Hilbert, Landau and Siegel.  First of all, any nonzero sum of squares in a number field has to be totally positive (that is, it is positive in all real embeddings).  Hilbert (1902) conjectured that in any number field, a totally positive element is a sum of 4 squares in the number field. This was proved by Landau (1919) for quadratic fields and by Siegel (1921) for all number fields. 
This sounds superficially like a direct extension of Lagrange's theorem, but there is a catch: it is about field elements, not algebraic integers as sums of squares of algebraic integers. A totally positive algebraic integer in a number field $K$ need not be a sum of 4 squares of algebraic integers in $K$. The Hilbert-Landau-Siegel theorem only says it is a sum of 4 squares of algebraic numbers in $K$. 
For instance, in $\mathbf{Q}(i)$ all elements are totally positive in a vacuous sense (no real embeddings), so every element is a sum of four squares.  As an example,
$$
i = \left(\frac{1+i}{2}\right)^2 + \left(\frac{1+i}{2}\right)^2.
$$
This shows $i$ is a sum of two squares in $\mathbf{Q}(i)$. 
It is impossible to write $i$ as a finite sum of squares in ${\mathbf Z}[i]$ since 
$$
(a+bi)^2 = a^2 - b^2 + 2abi
$$
has even imaginary part when $a$ and $b$ are in $\mathbf{Z}$.  Thus any finite sum of squares in $\mathbf{Z}[i]$ has even imaginary part, so such a sum can't equal $i$.
Therefore it is false that every totally positive algebraic integer in a number field is a sum of 4 squares (or even any number of squares) of algebraic integers.
Here are some further examples:

In $\mathbf{Q}(\sqrt{2})$, $5 + 3\sqrt{2}$ is totally positive since 
$5+3\sqrt{2}$ and $5-3\sqrt{2}$ are both positive.  So it must be a sum of at most four squares in this field by Hilbert's theorem, and with a little fiddling around you find 
$$
5 + 3\sqrt{2} = (1+\sqrt{2})^2 + \left(1 + \frac{1}{\sqrt{2}}\right)^2 + \left(\frac{1}{2}\right)^2 + \left(\frac{1}{2}\right)^2.
$$
It is impossible to write $5 + 3\sqrt{2}$ as a sum of squares in the ring of integers $\mathbf{Z}[\sqrt{2}]$ because of the parity obstruction we saw for $i$ as a sum of squares in $\mathbf{Z}[i]$: the coefficient of $\sqrt{2}$ in $5 + 3\sqrt{2}$ is odd.
In $\mathbf{Q}(\sqrt{2})$, $\sqrt{2}$ is not totally positive (it becomes negative when we replace $\sqrt{2}$ with $-\sqrt{2}$), so it can't be a sum of squares in this field. But in the larger field $\mathbf{Q}(\sqrt{2},i)$, everything is totally positive in a vacuous sense so everything is a sum of at most four squares in this field by the Hilbert-Landau-Siegel theorem.  And looking at $\sqrt{2}$ in $\mathbf{Q}(\sqrt{2},i)$, we find 
$$
\sqrt{2} = \left(1 + \frac{1}{\sqrt{2}}\right)^2 + i^2 + \left(\frac{i}{\sqrt{2}}\right)^2.
$$

Hilbert made his conjecture on totally positive numbers being sums of four squares as a theorem, in his Foundations of Geometry. It is Theorem 42. He says the proof is quite hard, and no proof is included. A copy of the book (in English) is available at the time I write this as http://math.berkeley.edu/~wodzicki/160/Hilbert.pdf. See page 83 of the file (= page 78 of the book).
Siegel's work on this theorem/conjecture was done just before the Hasse-Minkowski theorem was established in all number fields (by Hasse), and the former can be regarded as a special instance of the latter. 
Indeed, for nonzero $\alpha$ in a number field $K$, consider the quadratic form $$Q(x_1,x_2,x_3,x_4,x_5) = x_1^2+x_2^2+x_3^2+x_4^2-\alpha{x}_5^2.$$ To say $\alpha$ is a sum of four squares in $K$ is equivalent to saying $Q$ has a nontrivial zero over $K$. (In one direction, if $\alpha$ is a sum of four squares over $K$ then $Q$ has a nontrivial zero over $K$ where $x_5 = 1$. In the other direction, if $Q$ has a nontrivial zero over $K$ where $x_5 \not= 0$ then we can scale and make $x_5 = 1$, thus exhibiting $\alpha$ as a sum of four squares in $K$. If $Q$ has a nontrivial zero over $K$ where $x_5 = 0$ then the sum of four squares quadratic form represents 0 nontrivially over $K$ and thus it is universal over $K$, so it represents $\alpha$ over $K$.) By Hasse-Minkowski, $Q$ represents 0 nontrivially over $K$ if and only if it represents 0 nontrivially over every completion of $K$. 
Since any nondegenerate quadratic form in five or more variables over a local field or the complex numbers represents 0 nontrivially, $Q$ represents 0 nontrivially over $K$ if and only it represents 0 nontrivially in every completion of $K$ that is isomorphic to ${\mathbf R}$. The real completions of $K$ arise precisely from embeddings $K \rightarrow {\mathbf R}$. For $t \in {\mathbf R}^\times$, the equation 
$x_1^2+x_2^2+x_3^2+x_4^2-t{x}_5^2 =0$ has a nontrivial real solution if and only if $t > 0$, so $Q$ has a nontrivial representation of 0 in every real completion of $K$ if and only if $\alpha$ is positive in every embedding of $K$ into ${\mathbf R}$, which is what it means for $\alpha$ to be totally positive. (Strictly speaking, to be totally positive in a field means being positive in every ordering on the field. The orderings on a number field all arise from embeddings of the number field into $\mathbf R$, so being totally positive in a number field is the same as being positive in every real completion.)
Siegel's paper is "Darstellung total positiver Zahlen durch Quadrate, Math. Zeit. 11 (1921), 246--275, and can be found online at http://gdz.sub.uni-goettingen.de/en/dms/loader/img/?PPN=PPN266833020_0011&DMDID=DMDLOG_0022.

REPLY [17 votes]: K. Conrad's answer shows that one must, in general, make a distinction between rational representations of sums of squares and integral representations of sums of squares and that the latter is significantly more subtle.
Still, a lot of work has been done.  (I myself am familiar with only a little of it.)  The following classic paper gives comprehensive results for imaginary quadratic fields:

Niven, Ivan
  Integers of quadratic fields as sums of squares.
  Trans. Amer. Math. Soc. 48, (1940). 405--417. 

http://math.uga.edu/~pete/Niven40.pdf
Niven shows that the obstruction pointed out by Conrad is essentially the only one to representing integers in an imaginary quadratic field as sums of squares.  More precisely:
Let $m$ be a squarefree positive integer, and put $K = \mathbb{Q}(\sqrt{-m})$, $\mathbb{Z}_K$ the ring of integers of $K$.
Case 1: $m \equiv 1 \pmod 4$.  In this case $\mathbb{Z}_K = \mathbb{Z}[\sqrt{-m}]$ and there is an obstruction as above.  Namely, an element $a + b \sqrt{-m}$ is a sum of squares in $\mathbb{Z}_K$ iff it is a sum of $3$ squares in $\mathbb{Z}_K$ iff $b$ is even.
Case 2: $m \equiv 3 \pmod 4$.  In this case $\mathbb{Z}_K = \mathbb{Z}[\frac{1+\sqrt{-m}}{2}]$ and the obstruction of the previous case disappears: every element of 
$\mathbb{Z}_K$ is a sum of 3 squares in $\mathbb{Z}_K$.

REPLY [8 votes]: For explicit results, see

Cohn, Harvey: 
Decomposition into four integral squares in the fields of 
$2^{1/2}$ and $3^{1/2}$, Amer. J. Math. 82, 301-322 (1960) 
(A different proof was given by J. Deutsch,
An alternate proof of Cohn's four squares theorem;
J. Number Theory 104, No. 2, 263-278 (2004)) 
Colliot-Thélène, Jean-Louis; Xu, Fei: 
 Brauer-Manin obstruction for integral points of homogeneous spaces and 
representation by integral quadratic forms
Compos. Math. 145, No. 2, 309-363 (2009)
(This one is a lot deeper and connects the representability with the Brauer-Manin
obstruction; I have only seen the review.)