# fact: Factorial Function In pracma: Practical Numerical Math Functions

## Description

Factorial for non-negative integers `n <= 170`.

## Usage

 ```1 2 3``` ```fact(n) factorial2(n) ```

## Arguments

 `n` Vector of integers, for `fact`, resp. a single integer for `factorial2`.

## Details

The factorial is computed by brute force; factorials for `n >= 171` are not representable as ‘double’ anymore.

## Value

`fact` returns the factorial of each element in `n`. If `n < 0` the value is `NaN`, and for `n > 170` it is `Inf`. Non-integers will be reduced to integers through `floor(n)`.

`factorial2` returns the product of all even resp. odd integers, depending on whether `n` is even or odd.

## Note

The R core function `factorial` uses the `gamma` function, whose implementation is not accurate enough for larger input values.

`factorial`

## Examples

 ```1 2 3 4 5 6 7 8 9``` ```fact(c(-1, 0, 1, NA, 171)) #=> NaN 1 1 NA Inf fact(100) #=> 9.332621544394410e+157 factorial(100) #=> 9.332621544394225e+157 # correct value: 9.332621544394415e+157 # Stirling's approximation: 9.324847625269420e+157 # n! ~ sqrt(2*pi*n) * (n/e)^n factorial2(8); factorial2(9); factorial2(10) # 384 945 3840 factorial(10) / factorial2(10) # => factorial2(9) ```

### Example output

```[1] NaN   1   1  NA Inf
[1] 9.332622e+157
[1] 9.332622e+157
[1] 384
[1] 945
[1] 3840
[1] 945
```

pracma documentation built on Dec. 11, 2021, 9:57 a.m.