Why does $(2/p)=\prod_{k=1}^{(p-1)/2}2\cos\left(\frac{2\pi k}{p}\right)$? Browsing one of my favorite video game sites, there was a post asking about the derivation of the formula
$$
\left(\frac{2}{p}\right)=\prod_{k=1}^{(p-1)/2} 2\cos(2\pi k/p).
$$
I know that $\left(\frac{2}{p}\right)=(-1)^{(p^2-1)/8}$, but I'd never seen a formula in terms of cosine, it seems computational more involved. Anyway, out of curiosity is there a derivation or reference to a derivation? Many thanks.

For odd prime $p,$
$$\cos px+i\sin px=(\cos x+i\sin x)^p=\sum_{0\le r\le p\cos^{p-r}x(i\sin x)^r}$$
Equating the Real parts, $$\cos px=\cos^px-\binom p2 \cos^{n-2}x\sin^2x+\binom p4\cos^4x\sin^4x+\cdots$$
$$=\cos^px(1+\binom p2+\binom p4+\cdots)+\cdots+(-1)^{\frac{p-1}2}\cos x$$
$$=2^{p-1}\cos^px+\cdots+(-1)^{\frac{p-1}2}\cos x$$
If we put $x=\frac{2k\pi}p,\cos px=1$ where $k$ is any integer.
So, the roots of $$2^{p-1}\cos^px+\cdots+(-1)^{\frac{p-1}2}\cos x-1=0$$ are $\cos \frac{2k\pi}p$ where $0\le k\le p-1$
So, $$\prod_{0\le k\le p-1}\cos \frac{2k\pi}p=\frac1{2^{p-1}}\implies \prod_{1\le k\le p-1}\cos \frac{2k\pi}p=\frac1{2^{p-1}}$$ as $\cos\frac{2k\pi}p=1$ for $k=0$
Now, $\cos\frac{2(p-k)\pi}p=\cos(2\pi-\frac{2k\pi}p)=\cos\frac{2k\pi}p\implies \cos\frac{2k\pi}p\cos\frac{2(p-k)\pi}p=\cos^2\frac{2k\pi}p$
$$\implies \prod_{1\le k\le p-1}\cos \frac{2k\pi}p=\prod_{1\le k\le \frac{p-1}2}\cos\frac{2k\pi}p\prod_{\frac{p-1}2< k\le p-1}\cos\frac{2k\pi}p=\prod_{1\le k\le \frac{p-1}2}\cos^2\frac{2k\pi}p$$
So, $$\prod_{1\le k\le \frac{p-1}2}\cos^2\frac{2k\pi}p=\frac1{2^{p-1}}\implies \prod_{1\le k\le \frac{p-1}2}\left(2\cos\frac{2k\pi}p\right)^2=1$$
Now, $\cos\frac{2k\pi}p<0$ if $\frac\pi2<\frac{2k\pi}p<3\frac\pi2\implies \frac p4< k<\frac{3p}4$ which in our case reduces to $\frac p4< k\le \frac{p-1}2$
So $k$ has $\mu=\frac{p-1}2-\lfloor \frac p4\rfloor$ values for which $\cos\frac{2k\pi}p<0$, 
$$(-1)^\mu\prod_{1\le k\le \frac{p-1}2}2\cos\frac{2k\pi}p=1\implies \prod_{1\le k\le \frac{p-1}2}2\cos\frac{2k\pi}p=(-1)^\mu$$
Observe that $\mu,\frac{p^2-1}8$ have same parities.
For example, for $p=8n-1, \frac{p^2-1}8=\frac{(8n-1)^2-1}8=8n^2-2n$ and $\mu=4n-1-(2n-1)=2n$
