fact -- the factorial function
Introductionfact(n) represents the factorial
n!=1*2*3*..*n of an integer.
Call(s)fact(n)
n!
Parametersn |
- | an arithmetical expression representing a nonnegative integer |
Returnsan arithmetical expression.
n
Related
Functions
Detailsn! is equivalent to
fact(n).n is a nonnegative integer, then an integer is
returned. If n is a numerical value of some other type,
then an error occurs. If n is a symbolic expression, then
a symbolic call of fact is returned.gamma function
generalizes the factorial function to arbitrary complex arguments. It
satisfies gamma(n+1) = n! for nonnegative integers
n. Expressions involving symbolic fact calls
can be rewritten by rewrite(expression, gamma). Cf.
example 3.! can also be used in prefix notation
with an entirely different meaning: !command is equivalent
to system("command").fact is a function of the system kernel.
Example
1Integer numbers are produced if the argument is a nonnegative integer:
>> fact(0), fact(5), fact(2^5)
1, 120, 263130836933693530167218012160000000
A symbolic call is returned if the argument is a symbolic expression:
>> fact(n), fact(n - sin(x)), fact(3.0*n + I)
fact(n), fact(n - sin(x)), fact(3.0 n + I)
The calls fact(n) and
n! are equivalent:
>> 5! = fact(5), (n^2 + 3)!
2
120 = 120, fact(n + 3)
A numerical argument produces an error if it is not a positive integer:
>> fact(3/2 + I)
Error: Non-negative integer expected [specfunc::fact];
during evaluation of 'fact'
Example
2Use gamma(float(n+1)) rather
than float(fact(n)) for floating point approximations of
large factorials. This avoids the costs of computing large integer
numbers:
>> float(fact(2^13)) = gamma(float(2^13 + 1))
1.275885799e28503 = 1.275885799e28503
Example
3The functions expand, limit, rewrite and series handle expressions involving
fact:
>> expand(fact(n^2 + 4))
2 2 2 2 2
fact(n ) (n + 1) (n + 2) (n + 3) (n + 4)
>> limit(fact(n)/exp(n), n = infinity)
infinity
>> rewrite(fact(2*n^2 + 1)/fact(n - 1), gamma)
2
gamma(2 n + 2)
---------------
gamma(n)
The Stirling formula is obtained as an asymptotic series:
>> series(fact(n), n = infinity, 2)
n + 1 / 2 PI \1/2
(n + 1) exp(- n - 1) | ----- | +
\ n + 1 /
n + 1 / 2 PI \1/2
(n + 1) exp(- n - 1) | ----- |
\ n + 1 /
-------------------------------------- +
12 n
/ n + 1 / 2 PI \1/2 \
| (n + 1) exp(- n - 1) | ----- | |
| \ n + 1 / |
O| -------------------------------------- |
| 2 |
\ n /