@骄傲地蚂蚁 @happy丶2b青年 @cushaw80看一看这个,不是我原创的,但是速度还是比较快,计算1000位的阶乘:1000!
Function multi(ByVal X As String, ByVal Y As String) As String 'multi of two huge hexnum(两个大数之积)
Dim result As Variant
Dim xl As Long, yl As Long, temp As Long, i As Long
xl = Len(Trim(X))
yl = Len(Trim(Y))
ReDim result(1 To xl + yl)
For i = 1 To xl
For temp = 1 To yl
result(i + temp) = result(i + temp) + Val(Mid(X, i, 1)) * Val(Mid(Y, temp, 1))
Next
Next
For i = xl + yl To 2 Step -1
temp = result(i) \ 10
result(i) = result(i) Mod 10
result(i - 1) = result(i - 1) + temp
Next
If result(1) = "0" Then result(1) = ""
multi = Join(result, "")
Erase result
End Function
Private Sub Command1_Click() '节约时间,算到1000!
For i = 1 To 9
calcfactorial i * 100
Next
End Sub
Sub calcfactorial(ByVal n As Integer)
Dim a() As String, i As Long, stimer As Double
ReDim a(1 To n)
a(1) = 1
stimer = Timer
For i = 2 To n
a(i) = multi(a(i - 1), i)
Next
Debug.Print n & "! : 用时 "; Timer - stimer & " 秒, 结果 " & Len(a(n)) & " 位"
Debug.Print a(n)
End Sub