TITLE: Is every square root of an integer a linear combination of cosines of $\pi$-rational angles?
QUESTION [6 upvotes]: For example, $\sqrt 2 = 2 \cos (\pi/4)$, $\sqrt 3 = 2 \cos(\pi/6)$, and $\sqrt 5 = 4 \cos(\pi/5) + 1$. Is it true that any integer's square root can be expressed as a (rational) linear combinations of the cosines of rational multiples of $\pi$?
Products of linear combinations of cosines of rational multiples of $\pi$ are themselves such linear combinations, so it only needs to be true of primes. But I do not know, for example, a representation of $\sqrt 7$ in this form.

REPLY [23 votes]: Someone should actually record the formula. If $p$ is a prime $\equiv 1 \bmod 4$, then 
$$\sqrt{p} = \sum_{k=1}^{p-1} \left( \frac{k}{p} \right) \cos \frac{2 k \pi}{p}$$
where $\left( \tfrac{k}{p} \right)$ is the quadratic residue symbol. Note that $\left( \tfrac{k}{p} \right) = \left( \tfrac{p-k}{p} \right)$, so every term appears twice. 
Similarly, if $p \equiv 3 \bmod 4$, then 
$$\sqrt{p} = \sum_{k=1}^{p-1} \left( \frac{k}{p} \right) \sin \frac{2 k \pi}{p}.$$
Again, $k$ and $p-k$ make the same contribution. 
These are usually both written together as
$$\sqrt{(-1)^{(p-1)/2} p} = \sum_{k=1}^{p-1} \left( \frac{k}{p} \right) \exp \frac{2 k \pi i}{p}.$$
This is a formula of Gauss.

REPLY [11 votes]: Yes, that is true. The general case is the Kronecker-Weber theorem as Lucia mentioned in the comments. For square roots one can be more explicit and prove $\mathbb{Q}(\zeta_p) \cap \mathbb{R} = \mathbb{Q}[\sqrt{ (-1)^{(p-1)/2} p }]$ for odd prime numbers $p$ using properties of the Legendre symbol. Therefore you can write $\sqrt{p}$ either as $\mathfrak{Re}(\sqrt{p})$ or $\mathfrak{Im}(\sqrt{-p})$ depending on $p\mod 4$ so that you can write it as rational (in fact: integral) linear combination of real- or imaginary parts of powers of $\zeta_p$, i.e. cosine or sine values.