Exact value of Gauss Sum 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?
 A: Poisson summation formula, written appropriately, is applicable to some discontinuous functions. Namely, $$\sum_{m\in\mathbb{Z}}\frac{f(m^+)+f(m^-)}{2}=\sum_{m\in\mathbb{Z}}\widehat{f}(m),\qquad\widehat{f}(t):=\int_{\mathbb{R}}f(x)e^{-2\pi itx}\,dx,$$ where $f(m^+):=\lim\limits_{x\ \downarrow\ m}f(x)$ and $f(m^-):=\lim\limits_{x\ \uparrow\ m}f(x)$, holds if $f(x)$ is piecewise continuous and, say, has compact support and bounded variation. This is seen from a proof that uses the Fourier series for $$x\mapsto\sum_{m\in\mathbb{Z}}f(m+x).$$
Applied to $f(x)=\begin{cases}e^{2\pi ix^2/n},&0\leqslant x\leqslant n\\\hfill 0,\hfill&\text{otherwise}\end{cases}$, the sum on the LHS is exactly $S(n)$, and $$\widehat{f}(-m)=\int_0^ne^{2\pi i(x^2+mnx)/n}\,dx=i^{-m^2n}\sqrt{n}\int_{m\sqrt{n}/2}^{(m+2)\sqrt{n}/2}e^{2\pi iy^2}\,dy.$$ Summing over even and over odd values of $m$ separately, we obtain $$S(n)=(1+i^{-n})\sqrt{n}\int_{-\infty}^{\infty}e^{2\pi iy^2}\,dy=\frac{(1+i)(1+i^{-n})\sqrt{n}}{2},$$ as expected (after examining each value of $n\bmod 4$).
