今天又无聊的逛了一下搜搜问问,找到这样一个问题:
有谁能提供vbs里20的阶乘的详细程序
下面有人给出了这样的答案:
复制代码 代码如下:
function jx(x)
j=1
for i=2 to x
j=j*i
next
jx=j
end function
msgbox jx(20)
运行一下上面的程序,输出2.43290200817664E+18。笑而不语,再次证明了我之前的结论,在这种网站上回答问题的,大部分水平都不咋地。
其实之前在《用VBS精确计算2的100次方》我已经写过了VBS的大数乘法,调用一下就行了:
复制代码 代码如下:
'Author: Demon
'Website: http://demon.tw
'Email: 380401911@qq.com
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
不过,能写出这样的程序的人,不会无聊到去回答搜搜问问的问题吧。
原文:http://demon.tw/programming/vbs-factorial.html