Microsoft Small Basic

Program Listing: RDT140-0
'' Factorial 2012-04-20 WhTurner (RDT140-0)
''========================================================================
'' Adjustable parameters:
First= 190 '' first factorial in output (make equal to Last for only one factorial)
Last= 200 '' last factorial in output
Sdigit= 10 '' number of significant digits in scientific notation (1..12) - use 0(zero) for string output
Ts= " " ''Thousands separator ( " " space ; "," comma or "" nothing )
Ds= "." ''Decimal separator
''========================================================================
'' Var Init:
m[1]= 1
Mmax= 1
Empty= " "
''========================================================================
For i= 1 To Last
Multiply()

If i >= First Then ''output
Tout= m[Mmax]

For j= Mmax-1 To 1 Step -1
out= "x00000000"+m[j]
Lout= Text.GetLength(out)
out= Text.GetSubTextToEnd(out,Lout-5)
Tout= Text.Append(Tout,out)
EndFor

Lout= Text.GetLength(Tout)
L= Text.GetLength(i)
TextWindow.Write(Text.GetSubText(Empty,1,4-L)+i+"! = ")

If Sdigit = 0 Then ''print string
L= Math.Ceiling(Lout/3)*3-Lout
Tout= Text.Append(Text.GetSubText(Empty,1,L),Tout)
Lout= Lout+L

For j= 1 To Lout-3 Step 3
TextWindow.Write(Text.GetSubText(Tout,j,3)+Ts)
EndFor

TextWindow.WriteLine(Text.GetSubTextToEnd(Tout,Lout-2))

ElseIf Lout < Sdigit+1 Then ''scientific notation with Sdigit significant digits
TextWindow.WriteLine(Tout)

Else
Temp= Text.GetSubText(Tout,1,1+Sdigit)
Temp= Math.Round(Temp/10)
TextWindow.Write(Text.GetSubText(Tout,1,1)+Ds)
TextWindow.Write(Text.GetSubTextToEnd(Temp,2))
TextWindow.WriteLine(" * 10^"+(Lout-1))
EndIf

EndIf ''output
EndFor
''========================================================================
Sub Multiply
''in/output vars: m[1..Mmax] array for 6-digit parts
'' Mmax: maximum index of array; mult: multiplier
''local vars: M_i M_carry

For M_i= 1 To Mmax
m[M_i]= m[M_i]*i
EndFor

For M_i= 1 To Mmax
M_carry= Math.Floor(m[M_i]/1000000)
m[M_i]= Math.Remainder(m[M_i],1000000)
m[1+M_i]= M_carry+m[1+M_i]
EndFor

If m[Mmax+1] > 0 Then
Mmax= Mmax+1
EndIf

EndSub
''========================================================================