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)


' FB 1.05.0 Win64
 
#include "gmp.bi"
 
Sub leftFactorial(rop As __mpz_struct, op As ULong)
  Dim As __mpz_struct t1
  mpz_init_set_ui(@t1, 1)
  mpz_set_ui(@rop, 0)
  For i As ULong = 1 To op
    mpz_add(@rop, @rop, @t1)
    mpz_mul_ui(@t1, @t1, i)
  Next
  mpz_clear(@t1)
End Sub
 
Function digitCount(op As __mpz_struct) As ULong
  Dim As ZString Ptr t = mpz_get_str(0, 10, @op)
  Dim As ULong ret = Len(*t)
  Deallocate(t)
  Return ret
End Function
 
Dim As __mpz_struct t
mpz_init(@t)
 
For i As ULong = 0 To 110
  If i <= 10 OrElse i Mod 10 = 0 Then
    leftFactorial(t, i)
    gmp_printf(!"!%u = %Zd\n", i, @t)
  End If
Next
 
Print
 
For i As ULong = 1000 To 10000 Step 1000
  leftFactorial(t, i)
  Print "!"; Str(i); " has "; digitCount(t); " digits" 
Next
 
mpz_clear(@t)
Print
Print "Press any key to quit"
Sleep