Can we evaluate the exact form of $g\left(k,n\right)=\sum_{r=0}^{n-1}\exp\left(2\pi i\frac{r^{2}k}{n}\right) $ for general $k$ and $n$? For $k=1$, on MathWorld we have that $g\left(1,n\right)=\left\{ \begin{array}{cc} (1+i)\sqrt{n} & \ \text{when}\ n\equiv0\ \text{mod}\ 4\\ \sqrt{n} & \text{when}\ n\equiv1\ \text{mod}\ 4\\ 0 & \text{when}\ n\equiv2\ \text{mod}\ 4\\ i\sqrt{n} & \text{when}\ n\equiv3\ \text{mod}\ 4 \end{array}\right\} .$
I know how to generalize to all $k$ when $n=p$ is a prime number, but what do we do when $n$ is not prime? Is there a simple way to rewrite it using whether or not $k$ is a square? I have a suspicion it should be fairly close to the form above, any help is appreciated.
Thanks,