Question: How can I solve this indefinite integral for an arbitrary integer $n>0$?

$$ \int{x^n e^x dx}$$

I could partially integrate it for small $n$, but that's not really a solution.

---

**Edit:** (TB) This question is closely related to: [Is there a closed form solution for $\int x^n e^{cx}$?](https://math.stackexchange.com/questions/21516/is-there-a-closed-form-solution-for-int-xn-ecx), but it is more elementary, because $n$ is an integer here.

Answer: 
I find it a little difficult for me to guess the solution by trying several $n$. I would like to do it as following:

$$\begin{align}\int x^ne^xdx&=x^ne^x+(-1)n\int x^{n-1}e^xdx,\qquad n\geq 1\\
\int x^0e^xdx&=e^x\end{align}$$

Then you get the recurrence relation:

$$\begin{align}a\_n(x)&=x^ne^x+(-1)na\_{n-1}(x),\qquad n\geq 1\\
a\_0(x)&=e^x\end{align}$$

With the recursive formula, it may be easier to find the *pattern* of the result.
$ \int{x^ne^xdx}=e^x\sum^n\_{k=0}{{\left(-1\right)}^{n-k}\frac{n!}{k!}x^k}+C$

Solution:

$ \int{xe^xdx}=\ ?$

$ u=x\to \ \frac{du}{dx}=1\to du=dx$

$ dv=e^xdx\to \frac{dv}{dx}=e^x\to \ \int{\frac{dv}{dx}dx}=\int{e^xdx}+C{{\stackrel{C=0}{\longrightarrow}}}v=e^x$

$ \frac{duv}{dx}=v\frac{du}{dx}+u\frac{dv}{dx}\to \ \int{\frac{duv}{dx}dx}=\int{v\frac{du}{dx}dx}+\int{u\frac{dv}{dx}dx}+C\_a\to \int{udv}=uv-\int{vdu}+C\_b$

$ \int{xe^xdx}=xe^x-\int{e^xdx}+C\_1=xe^x-e^x+C=e^x\sum^1\_{k=0}{{\left(-1\right)}^{1-k}\frac{1!}{k!}x^k}+C$

$ \int{x^2e^xdx}=?$

$ u=x^2\to du=2xdx$

$ dv=e^xdx{{\stackrel{C=0}{\longrightarrow}}}v=e^x$

$ \int{x^2e^xdx}=x^2e^x-2\int{{xe}^xdx}+C=x^2e^x-2\left(xe^x-e^x+C\right)=x^2e^x-2xe^x+2e^x+C\_1=e^x\sum^2\_{k=0}{{\left(-1\right)}^{2-k}\frac{2!}{k!}x^k}+C\_1$

Now we suppose:

$ \int{x^ke^xdx}=e^x\sum^k\_{i=0}{{\left(-1\right)}^{k-i}\frac{k!}{i!}x^k+C}$

$ \int{x^{k+1}e^xdx=\ ?}$

$ u=x^{k+1}\to du=\left(k+1\right)x^kdx$

$ dv=e^xdx{{\stackrel{C=0}{\longrightarrow}v=e^x}}$

$ \int{x^{k+1}e^xdx=\ x^{k+1}e^x-\int{\left(k+1\right)x^ke^xdx}}+C=x^{k+1}e^x-\left(k+1\right)\int{x^ke^xdx}+C=x^{k+1}e^x-\left(k+1\right)e^x\sum^k\_{i=0}{{\left(-1\right)}^{k-i}\frac{k!}{i!}x^k+C}=e^x\sum^{k+1}\_{i=0}{{\left(-1\right)}^{k-i+1}\frac{(k+1)!}{i!}x^{k+1}+C}$

$ k+1=n$

$ \int{x^ne^xdx}=e^x\sum^n\_{i=0}{{\left(-1\right)}^{n-i}\frac{n!}{i!}x^n+C}$