在Python中如何写一个大整数的快速乘法程序
如下为vb代码:Public Function MbC(D1 As String, D2 As String) As String
Dim j1&, j2&, e&, d&, e1&
' 按列法计算C=A*B
m = Trim(D1): n = Trim(D2)
x = Len(m) \ 4: Y = Len(n) \ 4
m = String(4 * x + 4 - Len(m), "0") & m
n = String(4 * Y + 4 - Len(n), "0") & n
x = x + 1: Y = Y + 1
Dim A(), B()
ReDim A(1 To x): ReDim B(1 To Y)
For i1 = 1 To x
A(i1) = Val(Mid(m, i1 * 4 - 3, 4))
Next
For i2 = 1 To Y
B(i2) = Val(Mid(n, i2 * 4 - 3, 4))
Next
ma = x: mb = Y
MC = ma + mb
ReDim c(MC)
e1 = 0
j1 = ma: j2 = ma
For I = MC To 2 Step -1
If I <= ma Then j2 = I - 1
e = e1: e1 = 0
For J = j1 To j2
e = e + A(J) * B(I - J)
If e > 2040000000 Then '减少进位次数
e = e - 2040000000
e1 = e1 + 204000
End If
Next J
If j1 > 1 Then j1 = j1 - 1
base = 10000
d = e \ base
c(I) = e - d * base
If Len(c(I)) < 4 Then
c(I) = String(4 - Len(c(I)), "0") & c(I)
Else
c(I) = c(I)
End If
jc = c(I) & jc
e1 = e1 + d
Next I
jc = d & jc
MbC = qqdl(Trim(jc))
End Function
这个程序算几十万位的两个数的乘法得3个小时,太慢了!