At a mathematical competition in my country the following problem was proposed:
Compute $\displaystyle I=\int \frac{x^2-x+1}{e^x \sqrt{x^2+1}}\,dx$ where $x\in \mathbb{R}$.
I came up with the following solution, and this is also the solution presented in my book:
\begin{align} I &= \int e^{-x}\sqrt{x^2+1}\,dx-\int e^{-x} \frac{x}{\sqrt{x^2+1}}\,dx\\ &= \int e^{-x}\sqrt{x^2+1}\,dx-\int e^{-x}\left(\sqrt{x^2+1}\right)'\,dx \\ &= \int e^{-x}\sqrt{x^2+1}\,dx-e^{-x}\sqrt{x^2+1}-\int e^{-x}\sqrt{x^2+1}\,dx \\ &=-e^{-x}{\sqrt{x^2+1}}+C. \end{align}
At a first glance, this seemed all right because $\left(-e^{-x}{\sqrt{x^2+1}}\right)'=e^{-x}\,\frac{x^2-x+1}{\sqrt{x^2+1}}$, $\forall x \in \mathbb{R}$.
But then I thought that maybe it is not, because I am basically subtracting two antiderivatives, and as my topic from here shows (A "proof" for $0=1$ by integrating $\int \frac{dx}{x\ln x}$ by parts), this is not allowed. I went on to plug in this indefinite integral into WolframAlpha and it said that no result could be found in terms of elementary functions.
Yet, $(-e^{-x}{\sqrt{x^2+1}})'=e^{-x}\,\frac{x^2-x+1}{\sqrt{x^2+1}}$, $\forall x \in \mathbb{R}$ is true, so my antiderivative should be correct.
So, what I am asking is if the solution I presented is somehow correct, and if it is, then why it works, since to the best of my knowledge it shouldn't.