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
posted @ 2012-02-23 15:44  高级测试开发网  阅读(491)  评论(0编辑  收藏  举报
了解开发资源最新动态:https://seniortesting.club