factorial - Factorial of input - MATLAB (original) (raw)

Main Content

Syntax

Description

f = factorial([n](#bts27rr-1-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.

The factorial of n is commonly written in math notation using the exclamation point character as n!. Note that n! is not a valid MATLAB® syntax for calculating the factorial of n.

example

Examples

collapse all

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.

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

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

f = 1×4 uint64 row vector

               120               3628800         1307674368000   2432902008176640000

Input Arguments

collapse all

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

Tips

Limitations

Saturation

Extended Capabilities

expand all

The factorial function supports GPU array input with these usage notes and limitations:

For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).

Version History

Introduced before R2006a