<s>[INST] Integration - Evaluate $\int \frac{x^7+2}{(x^2+x+1)^2} \ dx$ 
Evaluate $$\int \frac{x^7+2}{(x^2+x+1)^2} \ dx$$

This problem is from G N Berman, no. 2056 (integrate using ostrogradsky's method). I referred to this question as well as this article but I could neither understand the method nor come up with a solution for this integral. Resorting to standard long division is very tedious and messy, so i refrained from doing that.
Any hints/solutions on solving this are appreciated.
 [/INST] Standard long division is not really that bad. I got $$\int (x^3-2x^2+x+2-\frac{4x^3+6x^2+5x}{(x^2+x+1)^2})dx$$
Integrate each terms. You can integrate the first four terms to get $\frac{x^4}{4}-\frac{2x^3}{3}+\frac{x^2}{2}+2x$ use Ostrogradsky's method on the last term.
Ostrogradsky's method

$Q(x)=(x^2+x+1)^2$, $Q_1(x)=Q_2(x)=x^2+x+1$, $P(x)=4x^3+6x^2+5x$, you can deduce that $P_1(x)=-x$, $P_2(x)=4x+1$. (Try to work it out yourself.) Therefore, the last term becomes $-\frac{x}{x^2+x+1}+\int( \frac{4x+1}{x^2+x+1})\ dx$. Can you continue from here?

Hint after Ostrogradsky's method:

 Split the integral into 2 parts: $\int(\frac{4x+2}{x^2+x+1}-\frac{1}{x^2+x+1})\ dx$. Use substitution on the remaining integrals.

My final solution after the substitution is:

 $$\frac{x^4}{4}-\frac{2x^3}{3}+\frac{x^2}{2}+2x+\frac{x}{x^2+x+1}-2\ln(x^2+x+1)+\frac{2\sqrt{3} \arctan(\frac{2x\sqrt{3}+\sqrt{3}}{3})}{3}+C$$

</s>