Finite sum involving cosines I was wondering whether anyone knows an analytic proof (the one I know comes from graph theory) of the following identity
$$
\sum _{i=1}^{n-1} \frac{1}{1-\cos \left(\frac{2\pi  i}{n}\right)} = \frac{n^2-1}{6}
$$
and its twin
$$
\sum _{i=1}^{n-1} \frac{1}{1-\cos \left(\frac{\pi  i}{n}\right)} = \frac{n^2-1}{3}.
$$
They seem related to this other trigonometric identity, but I don't know see how to derive them from that one.
 A: The method used here is taken from this answer. The idea is to consider a complex function $f(z)$ which evaluates to $\frac{1}{1-\cos(\theta)}$ when $z$ is a $n$th root of unity. If we then multiply $f$ with a function $g(z)$ that has residue $1$ at the roots of unity and which falls off fast enough to ensure that the sum of the residues of $fg$ is zero we then have that the sum we are after is minus the sum of the residues of $fg$ at $z=1$.

When $z=e^{i\theta}$ we have
$$\frac{1}{1-\cos(\theta)} = -\frac{2z}{(z-1)^2}$$
so this will be our $f(z)$. The function $g(z) = \frac{n}{z(z^n-1)}$ has a simple pole with residue $1$ at all the $n$th roots of unity. If we now consider the function $h(z) = f(z)g(z)$ then $|h(z)| \sim \frac{2n}{|z|^{n+2}}$ as $|z|\to \infty$ so $\lim_{R\to \infty}\oint_{|z|=R} h(z){\rm d}z  = 0$ and the sum of the residues of $h$ must vanish. Since the only poles of $h$ are at the $n$th roots of unity (as the pole of $g$ at $z=0$ cancels with the zero of $f$) we therefore have 
$$\sum_{k=1}^{n-1}\frac{1}{1-\cos\left(\frac{2\pi k}{n}\right)} = -\text{Res}\left[-\frac{2z}{(z-1)^2}\cdot \frac{n}{z(z^n-1)}; z=1\right] = \frac{n^2-1}{6}$$
