Accelerating the pace of engineering and science

# Documentation Center

• Trial Software

# factorial

Factorial of input

## Description

example

f = factorial(n) returns the product of all positive integers less than or equal to n, where n is a nonnegative integer value. If n is an array, then f contains the factorial of each value of n. The data type and size of f is the same as that of n.

## Examples

expand all

### 10!

f = factorial(10)
f =

3628800

### 22!

format long
f = factorial(22)
f =

1.124000727777608e+21

In this case, f is accurate up to 15 digits, 1.12400072777760e+21, because double-precision numbers are only accurate up to 15 digits.

Reset the output format to the default.

format

### Factorial of Array Elements

n = [0 1 2; 3 4 5];
f = factorial(n)
f =

1     1     2
6    24   120

### Factorial of Unsigned Integer Values

n = uint64([5 10 15 20]);
f = factorial(n)
f =

120    3628800   1307674368000   2432902008176640000

## Input Arguments

expand all

### n — Input valuesscalar, vector, or array of real, nonnegative integer values

Input values, specified as a scalar, vector, or array of real, nonnegative integers.

Example: 5

Example: [0 1 2 3 4]

Example: int16([10 15 20])

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

### Limitations

• The result is only accurate for double-precision values of n that are less than or equal to 21. A larger value of n produces a result that has the correct order of magnitude and is accurate for the first 15 digits. This is because double-precision numbers are only accurate up to 15 digits.

• For single-precision input, the result is only accurate for values of n that are less than or equal to 13. A larger value of n produces a result that has the correct order of magnitude and is accurate for the first 8 digits. This is because single-precision numbers are only accurate up to 8 digits.