Here is a piecewise function extended periodically with period $2\pi$. $$f(x)= \begin{cases} \pi + x & -\pi<x<-\frac{\pi}{2} \\ x & -\frac{\pi}{2}< x<\frac{\pi}{2} \\ \pi - x & \frac{\pi}{2}< x<\pi \\ \end{cases} $$
I know to find the $a_n$ coefficients of a Fourier Series I can rewrite this in the form $$a_n=\frac{1}{\pi}\int_{-\pi}^{\pi}f(x)\cdot\cos(nx)dx\\ =\frac{1}{\pi}\left(\int_{-\pi}^{-\frac{\pi}{2}}(\pi+x)\cdot\cos(nx)dx+\int_{-\frac{\pi}{2}}^{\frac{\pi}{2}}x\cdot\cos(nx)dx+\int_{\frac{\pi}{2}}^{\pi}(\pi-x)\cdot\cos(nx)dx\right)$$
I then note that the middle integrand is an odd function overall, and is being evaluated over such an interval that the result would be zero, so it can be discarded. Then, after evaluating the outer two integrals and using the facts that $\sin(n\pi)=0\:\forall\, n$, $\cos(-x)=\cos(x)$, and $\sin(-x)=-\sin(x)$, I discover a number of cancellations and end up with the following. $$a_n=\frac{2}{\pi n^2}\left(\cos(n\pi)-\cos(\frac{n\pi}{2})\right)$$
When I iterate through the $n$ inside the cosine functions only, I find that $a_n$ is always one of $\{\frac{-2}{\pi n^2},\frac{4}{\pi n^2},\frac{-2}{\pi n^2},\frac{0}{\pi n^2}\}$ where these values correspond to the multiples $\{1\cdot n,2\cdot n, 3\cdot n,4\cdot n\}$ respectively.
I can't find a way to express this so that I can rid myself of the cosine functions within the expression, I have only begun studying this and we can usually turn this into something like $\frac{2}{\pi n^2}(-1)^{n}$ or other similar expressions.
I've double and triple checked my integrations and I'm 99.99% sure I've made no mistakes on that front, any help would be appreciated.
If you read my comment, we have
$$\cos n\pi-\cos\frac{n\pi}2=(-1)^n-(-1)^{n/2}=\begin{cases}-1,&n\;\text{ is odd}\\{}\\0,&n=2\pmod 4\\{}\\2,&n=0\pmod 4\end{cases}$$