我刚写的,没优化,效率不很高:代码:Option ExplicitPrivate Type tagBIGNUM
d(0 To 10000) As Byte
l As Long
End TypePrivate Sub InitBN(bn As tagBIGNUM, ByVal n As Currency)
Dim i As Long
For i = 0 To bn.l
bn.d(i) = 0
Next
bn.l = -1
While n > 0
bn.l = bn.l + 1
bn.d(bn.l) = n Mod 10
n = n \ 10
Wend
End SubPrivate Function BN2Str(bn As tagBIGNUM) As String
Dim i As Integer
Dim s As String, b As Byte
b = Asc("0")
For i = 0 To bn.l
s = Chr(bn.d(i) + b) + s
Next
BN2Str = s
End FunctionPrivate Function BN2Int(bn As tagBIGNUM) As Currency
BN2Int = Val(BN2Str(bn))
End FunctionPrivate Sub BNMul(bn1 As tagBIGNUM, bn2 As tagBIGNUM, r As tagBIGNUM)
InitBN r, 0
Dim i As Long, j As Long, k As Integer
For i = 0 To bn1.l
For j = 0 To bn2.l
r.d(i + j) = r.d(i + j) + bn1.d(i) * bn2.d(j)
k = i + j
While r.d(k) >= 10
r.d(k + 1) = r.d(k + 1) + (r.d(k) \ 10)
r.d(k) = r.d(k) Mod 10
k = k + 1
Wend
If r.l < k Then r.l = k
Next
Next
End Sub'计算阶乘,n=2000有点慢。
Private Sub Form_Load()
'Dim a As tagBIGNUM, b As tagBIGNUM, c As tagBIGNUM
'Dim i As Long, k As Integer, j As Long
'For k = 1 To 100
' i = Rnd * 10000
' j = Rnd * 10000
' InitBN a, i
' InitBN b, j
' BNMul a, b, c
' If BN2Int(c) <> i * j Then Debug.Print "ERR!" Else Debug.Print "OK"
'Next
Dim a As tagBIGNUM, b As tagBIGNUM, t As tagBIGNUM
Dim i As Long, n As Long
n = 2000
InitBN a, 1
For i = 1 To n
InitBN b, i
BNMul a, b, t
a = t
Next
Debug.Print BN2Str(a)
End Sub
d(0 To 10000) As Byte
l As Long
End TypePrivate Sub InitBN(bn As tagBIGNUM, ByVal n As Currency)
Dim i As Long
For i = 0 To bn.l
bn.d(i) = 0
Next
bn.l = -1
While n > 0
bn.l = bn.l + 1
bn.d(bn.l) = n Mod 10
n = n \ 10
Wend
End SubPrivate Function BN2Str(bn As tagBIGNUM) As String
Dim i As Integer
Dim s As String, b As Byte
b = Asc("0")
For i = 0 To bn.l
s = Chr(bn.d(i) + b) + s
Next
BN2Str = s
End FunctionPrivate Function BN2Int(bn As tagBIGNUM) As Currency
BN2Int = Val(BN2Str(bn))
End FunctionPrivate Sub BNMul(bn1 As tagBIGNUM, bn2 As tagBIGNUM, r As tagBIGNUM)
InitBN r, 0
Dim i As Long, j As Long, k As Integer
For i = 0 To bn1.l
For j = 0 To bn2.l
r.d(i + j) = r.d(i + j) + bn1.d(i) * bn2.d(j)
k = i + j
While r.d(k) >= 10
r.d(k + 1) = r.d(k + 1) + (r.d(k) \ 10)
r.d(k) = r.d(k) Mod 10
k = k + 1
Wend
If r.l < k Then r.l = k
Next
Next
End Sub'计算阶乘,n=2000有点慢。
Private Sub Form_Load()
'Dim a As tagBIGNUM, b As tagBIGNUM, c As tagBIGNUM
'Dim i As Long, k As Integer, j As Long
'For k = 1 To 100
' i = Rnd * 10000
' j = Rnd * 10000
' InitBN a, i
' InitBN b, j
' BNMul a, b, c
' If BN2Int(c) <> i * j Then Debug.Print "ERR!" Else Debug.Print "OK"
'Next
Dim a As tagBIGNUM, b As tagBIGNUM, t As tagBIGNUM
Dim i As Long, n As Long
n = 2000
InitBN a, 1
For i = 1 To n
InitBN b, i
BNMul a, b, t
a = t
Next
Debug.Print BN2Str(a)
End Sub
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货