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.
 A: The answer I am writing was already given and for some reason deleted by ryu jin. It seemed like a good answer to me, so here it is again as CW.
There is a more general theorem: For $a$ relatively prime to $p$, we have
$$\left( \frac{a}{p} \right) = \prod_{k=1}^{\frac{p-1}{2}} \frac{\sin (2 \pi ak/p)}{\sin (2 \pi k/p)}.$$
Plugging in $a=2$ and the identity $\sin(2 \theta) = 2 \sin \theta \cos \theta$ gives your formula.
Proof: For $a$ as above, and $k$ an integer in the interval $[1, (p-1)/2]$, we can reduce $ak$ modulo $p$ to get an integer in $[-(p-1)/2, (p-1)/2]$. Write that integer as $\epsilon(k) \phi(k)$ where $\epsilon(k)$ is either $1$ or $-1$ and $\phi(k) \in [1,(p-1)/2]$. Since $m \mapsto \sin( 2 \pi m/p )$ is periodic modulo $p$ and an odd function, we have
$$\prod_{k=1}^{\frac{p-1}{2}} \sin (2 \pi ak/p) = \prod_{k=1}^{\frac{p-1}{2}} {\Big(}\epsilon(k) \sin (2 \pi \phi(k)/p) {\Big)} = \prod_{k=1}^{\frac{p-1}{2}} \epsilon(k)  \prod_{k=1}^{\frac{p-1}{2}}  \sin (2 \pi \phi(k)/p)$$
By Gauss's Lemma,  we have $\prod_{k=1}^{(p-1)/2} \epsilon(k) = \left( \frac{a}{p} \right)$. Also by Gauss's lemma, the map $\phi$ is a bijection from $\{ 1,2, \ldots, (p-1)/2 \}$ to itself, so the second factor is $\prod_{k=1}^{(p-1)/2} \sin (2 \pi k/p)$.
So
$$\prod_{k=1}^{\frac{p-1}{2}} \sin (2 \pi ak/p) = \left( \frac{a}{p} \right) \prod_{k=1}^{(p-1)/2} \sin (2 \pi k/p)$$
as desired.
A: 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$
