# Show Factorial instead of Gamma in the result of RSolve

Now I wanted to solve for a recurring function with RSolve. Here's how I tried:

RSolveValue[{a[n] == n*a[n - 1] - n!, a == 2}, a[n], n]
(*2 Pochhammer[1, n] - n Pochhammer[1, n]*)
Simplify[%, Element[n, Integers]]
(* -(-2 + n) Pochhammer[1, n] *)
FunctionExpand[%]
(* -(-2 + n) n Gamma[n] *)


Actually the result should be $(2-n)n!$, but how can I simplify the result by assuming n be positive, so the result could look nicer.

• FullSimplify[n Gamma[n], Element[n, Integers] && n > 0] does simplify to n!. – Karsten 7. Nov 25 '15 at 8:17
• @Karsten, but FullSimplify[n Gamma[n] (2 - n), n ∈ Integers && n > 0] doesn't produce factorials, thus necessitating the use of ComplexityFunction. – J. M.'s torpor Nov 25 '15 at 8:30

## 2 Answers

You can increase penalty for Gamma and Pochhammer headers:

simplify[expr_, n_] :=
FullSimplify[expr, n ∈ Integers && n > 0,
ComplexityFunction -> ((LeafCount@# +
10 Count[#, _Gamma | _Pochhammer, {0, ∞}]) &)];

simplify[RSolveValue[{a[n] == n*a[n - 1] - n!, a == 2}, a[n], n], n]
(* -(-2 + n) n (-1 + n)! *)


Don't know if this suits your needs or not, but if you are certain that the argument to Gamma is a non-negative integer, then just make the replacement manually

RSolveValue[{a[n] == n*a[n - 1] - n!, a == 2}, a[n], n]
(* 2 Pochhammer[1, n] - n Pochhammer[1, n] *)

Simplify[%, Element[n, Integers]]
(* -(-2 + n) Pochhammer[1, n] *)

FunctionExpand[%]
(* -(-2 + n) n Gamma[n] *)

% /. n Gamma[n] -> Factorial[n]
(* -(-2 + n) n! *)

• n! == Pochhammer[1, n] == Gamma[n + 1] == n Gamma[n]. – J. M.'s torpor Nov 25 '15 at 8:41
• (face turns red) – Jason B. Nov 25 '15 at 8:42