<s>[INST] How do you integrate $\int \frac{1}{a + \cos x} dx$? How do you integrate $\int \frac{1}{a + \cos x} dx$?  Is it solvable by elementary methods?  I was trying to do it while incorrectly solving a homework problem but I couldn't find the answer.
Thanks!
 [/INST] Generalization:
Let's consider $\cos x = \frac{1-t^2}{1+t^2}; t = \tan\frac{x}{2}; dx=\frac{2}{1+t^2} dt.$ Then we get that our integral becomes:
$$J = \int \frac{2dt}{(a+b)+(a-b) t^2}$$
I. For the case $a>b$, consider $a+b=u^2$ and  $a-b=v^2$, and obtain that:
$$J = 2\int \frac{dt}{u^2+v^2 t^2}=\frac{2}{uv} \arctan\frac{vt}{u} +C.$$
Turning back to our notation we get:
$$I=\frac{2}{\sqrt{a^2-b^2}} \arctan\left(\sqrt{\frac{a-b}{a+b}} \tan\frac{x}{2} \right) + C.$$
II. For the case $a<b$, consider $a+b=u^2$ and  $a-b=-v^2$, and obtain that:
$$J = 2\int \frac{dt}{u^2-v^2 t^2}=\frac{1}{uv}\ln\frac{u+vt}{u-vt} \ +C.$$
Turning back again  to out initial notation and have that:
$$I=\frac{2}{\sqrt{b^2-a^2}} \ln\frac{b+a \cos x + \sqrt{b^2-a^2} \sin x}{a+b \cos x} + C.$$
Also, note that $x$ must be different from ${+}/{-}\arccos(-\frac{a}{b})+2k\pi$ if $|\frac{a}{b}|\leq1$.
Q.E.D.
</s>