## `fact` -- the factorial function

#### Introduction

`fact``(n)` represents the factorial n!=1*2*3*..*n of an integer.

#### Call(s)

`fact(n)`
`n!`

#### Parameters

`n` | - | an arithmetical expression representing a nonnegative integer

#### Returns

an arithmetical expression.

### Overloadable:

`n`

#### Related Functions

`gamma`, `igamma`, `psi`

#### Details

- The short hand call `n!` is equivalent to `fact``(n)`.
- If `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.
- Integer arguments must be smaller than 2^31 on 32-bit systems and smaller than 2^63 on 64-bit systems, respectively. Larger integers lead to an error.
- The `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.
- The operator `!` 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 1

Integer 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 2

Use `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 3

The 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                    /

#### Changes

- no changes

Do you have questions or comments?
Copyright © SciFace Software GmbH & Co. KG 2000