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 !n=\sum _{k=0}^{n-1}k!}




 where


   



!
0
=
0


{\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)


 
(* reuse earlier factorial calculations in dfac, apply to listed arguments in cumlfac *)
(* example: left factorial n, is #3 (dfac (0,n-1,1,1) ) *)
(* output list contains (number, factorial, left factorial) *)
(* tested in PolyML *)
 
 
val store = ref 0;
 
val rec dfac = fn 
        (from,to,acc,cm) => if from = to then (from,acc,cm) else (store:=(from+1)*acc;dfac (from+1,to,!store,!store+cm ) );
 
val rec cumlfac = fn 
        (x::y::rm) => x :: cumlfac ( dfac (#1 x, #1 y, #2 x, #3 x) :: rm ) |
        rm =>rm ;
 
val arguments = List.tabulate (10,fn 0=>(0,1,1)|i=>(i,0,0)) @ 
                List.tabulate (10,fn i=> (10*i+19,0,0) )    @ 
                List.tabulate ( 10,fn i=> (1000*i+999,0,0));
 
val result = (~1,0,0)::(cumlfac arguments);
 
(* done *)
(* display: *)
 
List.app (fn triple :int*int*int =>
        print(Int.toString (1+ #1 triple ) ^ " : " ^ Int.fmt StringCvt.DEC (#3 triple ) ^" \n" )
        ) (List.take(result,21)  ) ;
List.app (fn triple :int*int*int =>
        print( Int.toString (1+ #1 triple ) ^ " : " ^ Int.toString  (size(Int.toString (#3 triple ))) ^" \n" ) ) (List.drop(result,21)  );
 