user
**Left factorials**,   !n,   may refer to either   *subfactorials*   or to   *factorial sums*;
  
the same notation can be confusingly seen being used for the two different definitions.

Sometimes,   *subfactorials*   (also known as *derangements*)   may use any of the notations:

:   :   :   :   :   :   :   * **!*n*`**
                            * **!*n***
                            * ***n*¡**

(It may not be visually obvious, but the last example uses an upside-down exclamation mark.)

This Rosetta Code task will be using this formula   (*factorial sums*)   for   **left factorial**:

:   :   :   :   :   !
                    n
                    =

                    ∑

                    k
                    =
                    0

                    n
                    −
                    1
                    k
                    !
                    {\displaystyle {\displaystyle !n=\sum \_{k=0}^{n-1}k!}}

:   :   :   :   where

:   :   :   :   :   !
                    0
                    =
                    0
                    {\displaystyle {\displaystyle !0=0}}

Task

Display the left factorials for:

* zero through ten     (inclusive)
* 20   through   110   (inclusive)   by tens

Display the length (in decimal digits) of the left factorials for:

* 1,000   through   10,000   (inclusive), by thousands.

Also see

* The OEIS entry: A003422 left factorials
* The MathWorld entry: left factorial
* The MathWorld entry: factorial sums
* The MathWorld entry: subfactorial

Related task

* permutations/derangements (subfactorials)
assistant
(formerly Perl 6)

Implement left factorial as a prefix !. Note that this redefines the core prefix ! (not) function.

```
sub prefix:<!> ($k) { (constant l = 0, |[\+] 1, (|[\*] 1..*))[$k] }

$ = !10000; # Pre-initialize

.say for ( 0 … 10, 20 … 110 ).hyper(:4batch).map: { sprintf "!%d  = %s", $_, !$_ };
.say for (1000, 2000 … 10000).hyper(:4batch).map: { sprintf "!%d has %d digits.", $_, chars !$_ };

```

Output:

```
!0  = 0
!1  = 1
!2  = 2
!3  = 4
!4  = 10
!5  = 34
!6  = 154
!7  = 874
!8  = 5914
!9  = 46234
!10  = 409114
!20  = 128425485935180314
!30  = 9157958657951075573395300940314
!40  = 20935051082417771847631371547939998232420940314
!50  = 620960027832821612639424806694551108812720525606160920420940314
!60  = 141074930726669571000530822087000522211656242116439949000980378746128920420940314
!70  = 173639511802987526699717162409282876065556519849603157850853034644815111221599509216528920420940314
!80  = 906089587987695346534516804650290637694024830011956365184327674619752094289696314882008531991840922336528920420940314
!90  = 16695570072624210767034167688394623360733515163575864136345910335924039962404869510225723072235842668787507993136908442336528920420940314
!100  = 942786239765826579160595268206839381354754349601050974345395410407078230249590414458830117442618180732911203520208889371641659121356556442336528920420940314
!110  = 145722981061585297004706728001906071948635199234860720988658042536179281328615541936083296163475394237524337422204397431927131629058103519228197429698252556442336528920420940314
!1000 has 2565 digits.
!2000 has 5733 digits.
!3000 has 9128 digits.
!4000 has 12670 digits.
!5000 has 16322 digits.
!6000 has 20062 digits.
!7000 has 23875 digits.
!8000 has 27749 digits.
!9000 has 31678 digits.
!10000 has 35656 digits.
```

If you would rather not override prefix ! operator and you can live with just defining lazy lists and indexing into them, this should suffice; (and is in fact very slightly faster than the first example since it avoids routine dispatch overhead):

```
constant leftfact = 0, |[\+] 1, (|[\*] 1..*);

$ = leftfact[10000]; # Pre-initialize

.say for ( 0 … 10, 20 … 110 ).hyper(:4batch).map: { sprintf "!%d  = %s", $_, leftfact[$_] };
.say for (1000, 2000 … 10000).hyper(:4batch).map: { sprintf "!%d has %d digits.", $_, chars leftfact[$_] };

```

Same output.