From: Leroy Quet on
I'm going to generalize the main result now. (And I will cross-post
this to another related thread.)

m, n, and r are positive integers.
Let g = GCD(m,r).

If m|n and r|n, and if
GCD(n g /(m r), m /g) = 1,

then:

sum{1<=j<=n, GCD(j,n)=r} cos(2 pi j/m)

(= sum{1<=j<=n, GCD(j,n)=r} exp(i 2 pi j/m) )

= phi(n g/ (m r)) * mu(m/g),

where phi(k) is the number of positive integers <= k and coprime to k,
and where mu(k) is the Mobius function.

If m = n, we have the specific case:

sum{1<=j<=n, GCD(j,n)=r} cos(2 pi j/n)

= 0, if r doesn't divide n, of course,

= mu(n/r), if r|n.

Thanks,
Leroy Quet