Computing the indefinite integral $\int x^n \sin x\,dx$ $\newcommand{\term}[3]{
  \sum_{k=0}^{\lfloor #1/2 \rfloor} (-1)^{#2} x^{#3} \frac{n!}{(#3)!}
}$
I am trying to prove that for $n \in\mathbb N$,
$$
\int x^n \sin x \, dx
= \cos x \term{n}{k+1}{n-2k}
+ \sin x \term{(n-1)}{k}{n-2k-1}
$$
I started with differentiation, and this is what I got:
If we define $f(x)$ as
$$
f(x)
= \cos x \term{n}{k+1}{n-2k}
+ \sin x \term{n-1}{k}{n-2k-1}
$$
then we have
$$
\begin{align*}
f’(x)
&= \cos x \term{n}{k+1}{n-2k-1}
 - \sin x \term{n}{k+1}{n-2k} \\
&\qquad
 + \sin x \term{(n-1)}{k}{n-2k-2}
 + \cos x \term{(n-1)}{k}{n-2k-1} \\[8pt]
&= \cos x \left[
   \term{n}{k+1}{n-2k-1}
 + \term{(n-1)}{k}{n-2k-1}
\right] \\
&\qquad
 + \sin x \left[
   \term{(n-1)}{k}{n-2k-2}
 - \term{n}{k+1}{n-2k}
\right]
\end{align*}
$$
I don't know how to go on, because of the different limits of the sum with $\lfloor{n/2}\rfloor$ and $\lfloor{(n-1)/2}\rfloor$.
 A: I finally got my proof with differentiation finished, too.
If
$$f(x) = \sum_{k=0}^{\lfloor{n/2}\rfloor}(-1)^{k+1}x^{n-2k}{n!\over(n-2k)!}\cos x+\sum_{k=0}^{\lfloor{(n-1)/2}\rfloor}(-1)^kx^{n-2k-1}{n!\over(n-2k-1)!}\sin x$$
with $n\in \Bbb N$.
then
$$\begin{align}f'(x) &= \left(\sum_{k=0}^{\lfloor{n/2}\rfloor}(-1)^{k+1}x^{n-2k-1}{n!\over(n-2k-1)!}-\sum_{k=0}^{\lfloor{(n-1)/2}\rfloor}(-1)^{k+1}x^{n-2k-1}{n!\over(n-2k-1)!}\right)\cos x\\&{}\quad+\left(\sum_{k=0}^{\lfloor{(n-1)/2}\rfloor}(-1)^{k}x^{n-2k-2}{n!\over(n-2k-2)!}-\sum_{k=0}^{\lfloor{n/2}\rfloor}(-1)^{k+1}x^{n-2k}{n!\over(n-2k)!}\right)\sin x\end{align}$$
$$\begin{align}&= \left(\sum_{k=0}^{\lfloor{n/2}\rfloor}(-1)^{k+1}x^{n-2k-1}{n!\over(n-2k-1)!}-\sum_{k=0}^{\lfloor{(n-1)/2}\rfloor}(-1)^{k+1}x^{n-2k-1}{n!\over(n-2k-1)!}\right)\cos x\\&{}\quad+\left(\sum_{k=2}^{\lfloor{(n-1)/2}\rfloor+2}(-1)^{k}x^{n-2k+2}{n!\over(n-2k+2)!}-\sum_{k=1}^{\lfloor{n/2}\rfloor+1}(-1)^{k}x^{n-2k+2}{n!\over(n-2k+2)!}\right)\sin x\end{align}$$
$$\begin{align}&=\left(\sum_{k=0}^{\lfloor{n/2}\rfloor}(-1)^{k+1}x^{n-2k-1}{n!\over(n-2k-1)!}-\sum_{k=0}^{\lfloor{(n-1)/2}\rfloor}(-1)^{k+1}x^{n-2k-1}{n!\over(n-2k-1)!}\right)\cos x\\ &{}\quad+\left(\sum_{k=2}^{\lfloor{(n-1)/2}\rfloor+2}(-1)^{k}x^{n-2k+2}{n!\over(n-2k+2)!}-\sum_{k=2}^{\lfloor{n/2}\rfloor+1}(-1)^{k}x^{n-2k+2}{n!\over(n-2k+2)!}\right)\sin x\\ &{}\quad+ x^n\sin x\end{align}$$
Now we have to show that (1) $$ \sum_{k=0}^{\lfloor{n/2}\rfloor}(-1)^{k+1}x^{n-2k-1}{n!\over(n-2k-1)!}-\sum_{k=0}^{\lfloor{(n-1)/2}\rfloor}(-1)^{k+1}x^{n-2k-1}{n!\over(n-2k-1)!} = 0$$ and (2) $$\sum_{k=2}^{\lfloor{(n-1)/2}\rfloor+2}(-1)^{k}x^{n-2k+2}{n!\over(n-2k+2)!}-\sum_{k=2}^{\lfloor{n/2}\rfloor+1}(-1)^{k}x^{n-2k+2}{n!\over(n-2k+2)!}  = 0$$
For even numbers:
$$ \lfloor{n/2}\rfloor = n/2 $$
$$ \lfloor{(n-1)/2}\rfloor = (n-2)/2 $$
For odd numbers:
$$ \lfloor{n/2}\rfloor = (n-1)/2 $$
$$ \lfloor{(n-1)/2}\rfloor = (n-1)/2 $$
Also $n!$ is only defined for $$n\ge 0$$
(1) The odd case is trivial because of $$\lfloor{(n-1)/2}\rfloor = \lfloor{n/2}\rfloor$$
Even: We need $$ n -2k -1 \ge 0  $$ so
$$ k \le \lfloor{(n-1)/2}\rfloor = (n-2)/2 $$
We get:
$$ \sum_{k=0}^{(n-2)/2}(-1)^{k+1}x^{n-2k-1}{n!\over(n-2k-1)!}-\sum_{k=0}^{(n-2)/2}(-1)^{k+1}x^{n-2k-1}{n!\over(n-2k-1)!} = 0$$
With the same argumentation you can show (2).
It remains:
$$= 0\cos x+0\sin x+ x^n\sin x= x^n\sin x$$
A: Well.. I think I have a different way
$$ \int_a^b e^{izt} dz = \frac{e^{izt} }{it} |_{z=a}^{z=b}$$
Differentiate both sides with $t$ n times (*) and apply leibniz product rule(**) and some rearranging::
$$ \int_a^b z^n e^{izt} dz = \frac{1}{i^{n+1}}  \sum_{k=0}^n \frac{1}{t^{k+1}} \binom{n}{k} (-1)^k k! (iz)^{n-k} e^{izt} |_{z=a}^{z=b}$$
Simplfying and putting t=1:
$$ \int_a^b z^n e^{iz} dz= \sum_{k=0}^n \frac{n!}{(n-k)!} (i)^{k-1} z^{n-k} e^{iz} |_{z=a}^{z=b}$$
Let's see if this formula really works, sub in $a=0$ and $b= \frac{\pi}{2}$ and $n=2$:
$$ \int_0^{\frac{\pi}{2}} z^2 e^{iz} dz= \sum_{k=0}^2 \frac{2!}{(2-k)!} (i)^{k-1} z^{2-k} e^{iz} |_{z=0}^{z= \frac{\pi}{2}}= 2!e^{iz} \left[  \frac{-iz^2}{2!} + \frac{z}{1!} + \frac{i}{0!}\right]_{0}^{\frac{\pi}{2} }=e^{iz} \left[ 2i + 2z -iz^2  \right]_0^{\frac{\pi}{2} }= i(2i + \pi - i\frac{\pi^2}{4}) - (2i)=( \frac{\pi^2}{4} -2)+i(\pi -2) $$
Hence, by taking imaginary on both sides we get:
$$ \int_0^{\frac{\pi}{2} } z^2 sin(z) dz = \pi -2$$
And similar results for cosine :)
P.s: I nominate the above result to be called the trigonometric gamma function (incase this wasn't discovered before)
Oh btw I this also gives the results for $x^n sin(ax)$ :P
*: Feynman's trick
**: Leibniz product rule
A: Integrate by parts twice, and use two inductions (odd and even case). You may be able to unite these if you are skillful.
