# factorial

Factorial of symbolic input

## Description

example

f = factorial(n) returns the factorial of n. If n is an array, factorial acts element-wise on n.

## Examples

collapse all

Compute the factorial for a symbolic number.

f = factorial(sym(20))
f = $2432902008176640000$

Compute the factorial function for a symbolic expression. factorial returns exact symbolic output as the function call.

syms n
expr = n^2 + 1;
f = factorial(expr)
f = $\left({n}^{2}+1\right)!$

Calculate the factorial for a value of n = 3. Substitute the value of n by using subs.

fVal = subs(f,n,3)
fVal = $3628800$

Differentiate an expression containing the factorial function $\left({\mathit{n}}^{2}+\mathit{n}+1\right)!$

syms n
f = factorial(n^2 + n + 1)
f = $\left({n}^{2}+n+1\right)!$
df = diff(f)
df = $\left({n}^{2}+n+1\right)! \psi \text{psi}\left({n}^{2}+n+2\right) \left(2 n+1\right)$

The derivative of the factorial function is expressed in terms of the psi function.

Expand an expression containing the factorial function.

syms n
f = factorial(n^2 + n + 1);
f1 = expand(f)
f1 = $\left({n}^{2}+n\right)! \left({n}^{2}+n+1\right)$

Compute the limit at infinity for an expression containing the factorial function.

syms n
f = factorial(n)/exp(n);
fLim = limit(f,n,Inf)
fLim = $\infty$

Compute factorial for array input. factorial acts element-wise on array input.

A = sym([1 3; 4 5]);
f = factorial(A)
f =

$\left(\begin{array}{cc}1& 6\\ 24& 120\end{array}\right)$

## Input Arguments

collapse all

Input, specified as a number, vector, matrix, or array, or a symbolic number, variable, array, function, or expression.

collapse all

### Factorial Function

The factorial of a number n is defined as follows.

$n!=\prod _{k=1}^{n}k$

The factorial of 0 is 1.

## Tips

• Calling factorial for a number that is not a symbolic object invokes the MATLAB® factorial function.

## Version History

Introduced in R2012a