When I was studying quadratic reciprocity, my number theory professor used the following result without proof:
$$S(n)=\sum^{n-1}_{x=0}\exp\left(\frac{2\pi ix^2}{n}\right)=\begin{cases} \sqrt{n}+\sqrt{n}i &&\text{if }n\equiv0 \mod 4\\ \sqrt n && \text{if } n\equiv1 \mod4\\ 0 && \text{if } n\equiv2 \mod 4\\ i\sqrt n&&\text{if } n\equiv3 \mod 4 \end{cases} $$
Here $n$ is a positive integer. My professor said it can be done by complex analysis, so I try to prove it. I have read Stein's Complex Analysis so I know about the theory of sum of 2 squares, that $$r_2(n)=4(d_1(n)+d_2(n))$$
I try to compute $S(n)^2$ and there will be an $x^2+y^2$ in the exponential. However, I found a great difficulty on counting.
I also try using theta function (essentially Poisson Summation Formula), but the $S(n)$ is a finite sum rather than an infinite sum. So I cannot get rid of the remaining parts. Can someone please prove the formula using complex analysis?