factorial

Factorial n! of nonnegative integers.

Category: Mathematics > Elementary Math

Syntax

f = factorial(n)

Returns the product 1*2*...*n for each element of n.

Description

Factorials grow very fast. In double precision factorial(170) is about 7.26e306 and factorial(171) already overflows to Inf. Beyond factorial(21) the result is no longer an exact integer in a double, only the nearest representable value.

For non-integer or negative arguments, the generalization is gamma(n + 1).

Input arguments

n
Array of nonnegative integers (stored in any numeric class).

Output arguments

f
Array of the same size as n. factorial(0) is 1.

Example

Small and large factorials

factorial([0 1 5 10])
factorial(171)
Output
ans = 1×4

           1           1         120     3628800
ans = Inf

See also

gamma nchoosek prod gammaln