VBS函数应用--求大数的阶乘的算法
Option Explicit
Function multiple(byVal x, byVal y)
Dim n, t, i, j, z, w()
n = Len(x) - 1
t = Len(y) - 1
ReDim w(n + t + 1)
x = CStr(x) : y = CStr(y)
For i = 0 To UBound(w)
w(i) = "0"
Next
For i = 0 To t
Dim c : c = 0
Dim uv : uv = 0
For j = 0 To n
uv = (w(i+j)-"0") + c + _
(Mid(x,n-j+1,1)-"0") * (Mid(y,t-i+1,1)-"0")
w(i+j) = CStr(uv Mod 10 + "0")
c = uv \ 10
Next
w(i+n+1) = CStr(uv \ 10 + "0")
Next
z = Join(w,"")
z = StrReverse(z)
Do While Left(z,1) = "0"
z = Mid(z,2)
Loop
multiple = z
End Function
Function factorial(n)
Dim i, t : t = 1
For i = 1 To n
t = multiple(t, i)
Next
factorial = t
End Function
Dim t : t = Timer
WScript.Echo factorial(100)
WScript.Echo Timer - t
另外,dogfish也写了一个求1000阶乘的VBS:
dim digits(2568)
max_digit = 2568
digits(max_digit) = 1
for d=2 to 1000
for k=max_digit to ubound(digits)
digits(k) = digits(k) *d
next
k = ubound(digits)
while k>=max_digit
if digits(k)>10 then
digits(k-1) = digits(k-1) + fix(digits(k) / 10)
digits(k) = digits(k) mod 10
if k-1<max_digit then
max_digit = k - 1
end if
end if
k = k-1
wend
next
'把结果转为字串。
str = ""
for i=max_digit to ubound(digits)
str = str & digits(i)
next
msgbox str
作者:高级测试开发网
博客地址:https://seniortesting.club
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

浙公网安备 33010602011771号