# Integration by parts for deriving gamma function

Well, there is an integral that has quite a lot to do with $$n!$$, and that is the following :

$$f(n)$$ = Integrate[x^(n - 1)/E^x, {x, 0, Infinity}] =$$\int_0^{\infty } \frac{x^{n-1}}{e^x} \, dx$$ (1)

Integrate by parts the above gives

parts[u_, v_] := (#1*Integrate[#2, x] - Integrate[D[#1, x]*Integrate[#2, x], x] & )[u, v]

parts[x^(n - 1), E^(-x)] = $$-e^{-x} x^{n-1}-(n-1) \Gamma (n-1,x)$$

How to get this result ? ( see (1) too )

• To see the outcome of the integration in parts done in MMA in the form as noted in the box That's the goal. It is the factorial Mar 30, 2022 at 20:39

## Using RuleDelayed

Assuming[n > 0,
Integrate[x^(n - 1)/E^x, {x, 0, ∞}] /.
Gamma[x_] :> Factorial[x - 1]]


(-1 + n)!

## Using ComplexityFunction

gammatofac[x_, xx_] :=
FullSimplify[x, xx ∈ Integers && xx > 0,
ComplexityFunction -> ((LeafCount@# +
10 Count[#, _Gamma | _Pochhammer, {0, ∞}]) &)];


and run the integration

gammatofac[Integrate[x^(n - 1)/E^x, {x, 0, ∞}], n]


(-1 + n)!

• bmf, Yes, that is the correct outcome, cleverly done! Have seen this example as study text and the derivation is done manually. Your elaboration shows that this does involve specialized MMA knowledge which I as a regular user do not have. Mar 30, 2022 at 20:45
• bmf, Starting with the outcome of the partial integration. Can anything be derived from this? Mar 30, 2022 at 20:49
• @janhardo - "specialized MMA knowledge which I as a regular user do not have" ... You do if you use the readily available documentation. Mar 30, 2022 at 20:49
• @janhardo you could try for example FullSimplify[(n - 1) Gamma[n - 1], Element[n, Integers] && n > 0] but this gives back only Gamma[n].
– bmf
Mar 30, 2022 at 20:51
• @janhardo you could also try a simple replacement rule -see update.
– bmf
Mar 30, 2022 at 20:54
Clear["Global*"]


Your definition of parts does not consider the interval of integration. Change the definition to

parts[u_, dv_, interval_List] :=
Module[{v = Integrate[dv, x]},
Limit[u*v, x -> interval[[-1]]] -
Limit[u*v, x -> interval[[-2]]] -
Integrate[D[u, x]*v, interval]]

Assuming[n > 1, parts[x^(n - 1), E^-x, {x, 0, Infinity}]]

(* Gamma[n] *)

% /. Gamma[z_] :> (z - 1)! // TraditionalForm

(* (n-1)! *)
`
• Bob Hanlon- thanks ,I overlooked that the integration intervals when I used the code for the integration in parts. You did tackle it thoroughly by using this with module programming. Mar 30, 2022 at 21:56