Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

factorial

Factorial of input

Syntax

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.

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.

Examples

collapse all

f = factorial(10)
f =

     3628800

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
n = [0 1 2; 3 4 5];
f = factorial(n)
f =

     1     1     2
     6    24   120

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

  1×4 uint64 row vector

  Columns 1 through 3

                   120               3628800         1307674368000

  Column 4

   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

More About

collapse all

Tips

Limitations

  • For double-precision inputs, the result is exact when n is less than or equal to 21. Larger values of n produce 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 inputs, the result is exact when n is less than or equal to 13. Larger values of n produce 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.

Saturation

  • The table below describes the saturation behavior of each data type when used with the factorial function. The values in the last column indicate the saturation point; that is, the first positive integer whose actual factorial is larger than the maximum representable value in the middle column. For single and double, all values larger than the maximum value are returned as Inf. For the integer data types, the saturation value is equal to the maximum value in the middle column.

    Data typeMaximum ValueFactorial Saturation Threshold
    doublerealmaxfactorial(171)
    singlerealmax('single')factorial(single(35))
    uint64264-1factorial(uint64(21))
    int64263-1factorial(int64(21))
    uint32232-1factorial(uint32(13))
    int32231-1factorial(int32(13))
    uint16216-1factorial(uint16(9))
    int16215-1factorial(int16(8))
    uint828-1factorial(uint8(6))
    int827-1factorial(int8(6))

See Also

Introduced before R2006a

Was this topic helpful?