Problem: Prove that for any complex number $z \neq 1$, we have
\[
1 + z + z^2 + \dots + z^n = \frac{z^{n+1} - 1}{z - 1}.
\]
Using the above, prove that
\[
\sum_{k=0}^{n} \cos (k \theta ) = \frac{1}{2} + \frac{\sin \left( (n+\frac{1}{2}) \theta \right) }{2 \sin \left( \frac{\theta}{2} \right) }
\]
for $0 \lt \theta \lt 2\pi $.
Solution: Let $z$ be any complex number with $z \neq 1$. We need to prove that
\[
1 + z + z^2 + \dots + z^n = \frac{z^{n+1} - 1}{z - 1}.
\]
Consider
\begin{align*}
(z - 1) \left( 1 + z + \dots + z^n \right) & = \left( z + z^2 + z^3 + \dots + z^{n+1} \right) \\
& \kern 0.5 cm - \left( 1 + z + z^2 + \dots + z^n \right)
\\
& = z^{n+1} - 1.
\end{align*}
Since, $z \neq 1$, we can divide by $z - 1$ to the both sides. Therefore, we proved that
\[
1 + z + z^2 + \dots + z^n = \frac{z^{n+1} - 1}{z - 1}.
\]
Take $z = e^{\iota \theta }$. Since $0 \lt \theta \lt 2\pi $, $z \neq 1$. So from the first part,
\[
\sum_{k=0}^{n} z^k = \sum_{k = 0}^{n} \left( e^{\iota \theta } \right)^k = \frac{e^{(n+1)\iota \theta } - 1}{e^{\iota \theta } - 1}.
\]
Note that the real part of $\sum_{k=0} ^n e^{k \iota \theta }$ is equals to $\sum_{k=0}^{n} \cos (k \theta )$. So we need to evaluate the real part of $\frac{e^{(n+1)\iota \theta } - 1}{e^{\iota \theta } - 1}$.
\begin{align*}
\sum_{k=0}^{n} \left( e^{\iota \theta } \right)^k & = \frac{e^{(n+1)\iota \theta } - 1}{e^{\iota \theta } - 1} \\
& = \frac{e^{\iota n \theta } e^{\iota \theta } - 1}{e^{\iota \theta } - 1} \\
& = \frac{e^{\iota \theta /2}\left( e^{ \iota (n + \frac{1}{2})\theta } - e^{-\iota \frac{\theta}{2}} \right) }{e^{\iota \frac{\theta}{2} } \left( e^{\iota \frac{\theta}{2}} - e^{-\iota \frac{\theta}{2} } \right) } \\
& = \frac{e^{\iota (n + \frac{1}{2})\theta } - e^{-\iota \frac{\theta}{2} }}{2\iota \sin \left( \frac{\theta}{2} \right) } \\
& = \iota \left( \frac{e^{-\iota \frac{\theta}{2} } - e^{\iota (n + \frac{1}{2})\theta }}{2\sin \left( \frac{\theta}{2}\right) }\right) .
\end{align*}
The real part of $\iota \left( e^{-\iota \frac{\theta}{2} } - e^{\iota (n + \frac{1}{2})\theta } \right) $ is $-\sin (-\frac{\theta}{2} ) + \sin \left( n+\frac{1}{2} \right)\theta $. Therefore, we have the desired formula
\[
\sum_{k=0}^{n} \cos (k \theta ) = \frac{1}{2} + \frac{\sin \left( (n+\frac{1}{2}) \theta \right) }{2 \sin \left( \frac{\theta}{2} \right) }.
\]