user
Definitions
:   * The factorial of   **0**   (zero)   is defined as being   1   (unity).
    * The   **Factorial Function**   of a positive integer,    *n*,    is defined as the product of the sequence:

```
                 n,   n-1,   n-2,   ...   1 

```

Task

Write a function to return the factorial of a number.

Solutions can be iterative or recursive.

Support for trapping negative    *n*    errors is optional.

Related task

* Primorial numbers
assistant
(formerly Perl 6)

### via User-defined Postfix Operator

[\*] is a reduction operator that multiplies all the following values together. Note that we don't need to start at 1, since the degenerate case of [\*]() correctly returns 1, and multiplying by 1 to start off with is silly in any case.

**Works with**: Rakudo version 2015.12

```
sub postfix:<!> (Int $n) { [*] 2..$n }
say 5!;
```

Output:

```
120
```

### via Memoized Constant Sequence

This approach is much more efficient for repeated use, since it automatically caches. [\\*] is the so-called triangular version of [\*]. It returns the intermediate results as a list. Note that Raku allows you to define constants lazily, which is rather helpful when your constant is of infinite size...

**Works with**: Rakudo version 2015.12

```
constant fact = 1, |[\*] 1..*;
say fact[5]
```

Output:

```
120
```