TITLE: A trigonometric sum related with the primitive $q$-th roots of $1$
QUESTION [5 upvotes]: I am having problems proving the following:
For what values of $q$ does the following relationship hold
$$\sum_{\substack{1\leq d < q\\ \gcd(d,q)=1}}\!\!\!\cos\left(2\pi \cdot \frac{d}{q}\right) =0$$
I have found by computer analysis this may hold true for $q=p^r \cdot k$
where $p$ is any prime and $k$ is an integer and $r$ is an integer greater than one. For example $$q=4,8,9,12,16,18,20,24,25,27,28,32,36,40,44,45,48,49,50,52,54,56,60,63,64,68,72,75,76,80,81,84,88,90,92,96,98,99,100,104,108,112,116,117,120,121,124,125,126,128,132,135,136,140,144,147,148,150,152,153,156,160,162,164,168,169,171,172,175,176,180,184,188,189,192,196,198,200,204,207,208,212...$$
For instance for $q=12$ we have
$$\cos\left(2\pi\cdot \frac{1}{12}\right)+\cos\left(2\pi\cdot \frac{5}{12}\right)+\cos\left(2\pi\cdot \frac{7}{12}\right)+\cos\left(2\pi\cdot \frac{11}{12}\right)=0$$
But I am unable to prove this for the general case  $q=p^r\cdot k$. Any help would be greatly appreciated.

REPLY [8 votes]: Your sum is the real part of a Ramanujan's sum. Let us consider the cyclotomic polynomial $\Phi_q(x)$: its roots are given by the primitive $q$-th roots of unity, hence:
$$ \Phi_q(x) = \prod_{\substack{1\leq d \leq q \\ \gcd(d,q)=1}}\left(x-\exp\frac{2\pi i d}{q}\right)\tag{1} $$
and $\Phi_q(x)$ is a palyndromic polynomial, since if $\xi$ is a primitive $q$-th root of unity we have that $\xi^{-1}$ is a primitive $q$-th root of unity too. The degree of $\Phi_q(x)$ is $\varphi(q)$ and by Vieta's Theorem the following Ramanujan sum
$$ \sum_{\substack{1\leq d \leq q \\ \gcd(d,q)=1}}\!\!\!\exp\frac{2\pi i d}{q} $$
is given by the opposite of the coefficient of $x^{\varphi(q)-1}$ in $\Phi_q(x)$, which equals the opposite of the coefficient of $x$ in $\Phi_q(x)$, i.e. $-\left.\frac{d}{dx}\log\Phi_q(x)\right|_{x=0}$. By Moebius inversion formula
$$ \Phi_q(x) = \prod_{d\mid q}\left(1-x^d\right)^{\mu\left(\frac{q}{d}\right)}\tag{2} $$
hence:
$$ \frac{d}{dx}\log\Phi_q(x)=\sum_{d\mid q}\frac{-d x^{d-1}}{1-x^d}\mu\left(\frac{q}{d}\right) \tag{3} $$
and by evaluating both sides at $x=0$ and exploiting Moebius inversion formula again, we get:
$$ \sum_{\substack{1\leq d \leq q \\ \gcd(d,q)=1}}\!\!\!\exp\frac{2\pi i d}{q} = \color{red}{\mu(q)}.\tag{4}$$
The RHS of $(4)$ is always real and we have that

$$\sum_{\substack{1\leq d \leq q \\ \gcd(d,q)=1}}\!\!\!\cos\frac{2\pi d}{q} = 1\quad \Longleftrightarrow\quad \begin{array}{c}q\text{ is a square-free number with}\\\text{an even number of prime factors}\end{array}$$
  $$\sum_{\substack{1\leq d \leq q \\ \gcd(d,q)=1}}\!\!\!\cos\frac{2\pi d}{q} = -1\quad \Longleftrightarrow\quad \begin{array}{c}q\text{ is a square-free number with}\\\text{an odd number of prime factors}\end{array}$$
  $$\sum_{\substack{1\leq d \leq q \\ \gcd(d,q)=1}}\!\!\!\cos\frac{2\pi d}{q} = 0\quad \Longleftrightarrow\quad q\text{ is not a square-free number.}\tag{5}$$