<s>[INST] Calculate the sum $\sum _{p=0}^{p=D}(-1)^{\frac{(p-1)(p-2)}{2}}\binom{D}{p}$ Calculate the sum $\sum _{p=0}^{p=D}(-1)^{\frac{(p-1)(p-2)}{2}}\binom{D}{p}$
I would like to calculate this sum. So far , I tried to write the factor $(-1)^{\dots}$ as the real part of a complex number but I can't still calculate it. 
 [/INST] For an integer $k$, $\binom{k-1}{2}=\frac{(k-1)(k-2)}{2}$ is even iff $k\equiv1\pmod{4}$ or $k\equiv2\pmod{4}$.  Hence,
$$S_D:=\sum_{p=0}^D\,(-1)^{\binom{p-1}{2}}\,\binom{D}{p}=-1+\binom{D}{1}+\binom{D}{2}-\binom{D}{3}-\ldots\,.$$
We separate $S_D$ into even and odd partial sums
$$E_D:=-\sum_{p=0}^D\,(-1)^{p}\,\binom{D}{2p}=-1+\binom{D}{2}-\binom{D}{4}+\binom{D}{6}-\ldots$$
and
$$O_D:=\sum_{p=0}^D\,(-1)^{p}\,\binom{D}{2p+1}=\binom{D}{1}-\binom{D}{3}+\binom{D}{5}-\binom{D}{6}+\ldots\,.$$
It is clear via binomial expansion that $$E_D=-\left(\frac{(1+\text{i})^D+(1-\text{i})^D}{2}\right)=-\text{Re}\left((1+\text{i})^D\right)=-2^{\frac{D}{2}}\,\cos\left(\frac{\pi D}{4}\right)\,,$$ where $\text{i}:=\sqrt{-1}$.
Now, we note that 
$$\begin{align}
S_D&=-1+\left(\binom{D}{1}+\binom{D}{2}\right)-\left(\binom{D}{3}+\binom{D}{4}\right)+\left(\binom{D}{5}+\binom{D}{6}\right)-\ldots
\\
&=-1+\binom{D+1}{2}-\binom{D+1}{4}+\binom{D+1}{6}-\ldots=E_{D+1}\,.
\end{align}$$
In other words, $S_D=E_{D+1}=-\text{Re}\left((1+\text{i})^{D+1}\right)=-2^{\frac{D+1}{2}}\,\cos\left(\frac{\pi (D+1)}{4}\right)$.  That is, 
$$S_D=\left\{
\begin{array}{ll}
(-1)^{\frac{D-4}{4}}\,2^{\frac{D}{2}}\,,&\text{if }D\equiv 0\pmod{4}\,,\\
0\,,&\text{if }D\equiv 1\pmod{4}\,,\\
(-1)^{\frac{D-2}{4}}\,2^{\frac{D}{2}}\,,&\text{if }D\equiv 2\pmod{4}\,,\\
(-1)^{\frac{D-3}{4}}\,2^{\frac{D+1}{2}}\,,&\text{if }D\equiv 3\pmod{4}\,.
\end{array}
\right.$$

On the other hand, it is also not difficult to see that $$O_D=\frac{(1+\text{i})^D-(1-\text{i})^D}{2\text{i}}\,.$$ That means $$\begin{align}S_D&=E_D+O_D=\frac{(-1-\text{i})(1+\text{i})^D+(-1+\text{i})(1-\text{i})^D}{2}\\&=-\left(\frac{(1+\text{i})^{D+1}+(1-\text{i})^{D+1}}{2}\right)=-\text{Re}\left((1+\text{i})^{D+1}\right)\,,\end{align}$$ which is the same as before.

</s>