2678145490787694710138406683675886762424440548647327525594133873038347266950777818028529804877891353347244255046176220880989519574806598935727647635506939520211209794328712328126947740602255036264392628020291100698432900022333877728824270241063030019536831205264726059774673235680796770108334187808344470141093691232926021244583649747591446066921535333854157441330339217364515328375889937313870826987520723914810358707312468857074455945610038118763873992681688857200178452843740327143990247797893489008808544535536081089994800692174759199578596435124153437689121057209523619989593900355037400039597165241729693472001589594642879166693443994939359639600558001937547209387175085119128575833396037478944824322315197771750730768383267287661855880237549141571520374996029107650341071074081501632143441365461865431362023790463469688071768208320226561675630348708531708110373889241713511809029065048200924851629507265095151843687852114948736130517024578482059771284515915597919026074110811882700307150650858080695566672584075215926450360584738705106755945308199980660478183563586945902705730231409948938478910176292179410952310592864745025373902249844607078729230098512959498522851990040833671698873183076996740726479481352429756295966452788332789259316653805673821716832204368483794501474641036177256005810416507649770210480747056764697918800240201155395628561684853684747063434500664449114760179367086926249813006056735202795763742271472136500817313178050955623517148960973128433455798444839070215800699075256371341531892841721456473301085595261371522078416420399702683504171019191363372883472415693921509069484448792009627325245658854979159002549542769741504596589556474585723101403486046952706847309188488390899096649210853675228464611999630360164703837131620319658277117300208326944026347808645954842782309633205645471959842390075986572806483755223497552549948718545978420793425036666212191832327909389117021539426917904222971515424022606157676379417252939026404363841301595348953340657745965591468370831371449860774296757556463049237248472613472436666799615694672489600751763956185893123579218797539971253828850163437053164723845196089466298000486042246621634263064752906475198879616484926366053597071510886820878425907736453843197228282179943365206039514850477377542684361730162273246709577442085159313947668552462343596819299335060863266863059421286080919061657175757802930378698062290163092494909661982639303946271889619023417245433500782685534406291391527022657608608130680134939380126048353657648559735671562230714451258764762350451448379438849319710473631625533527602085050858633346936315115309333355986556546113134787213610042970405680162084678021281774169131725296977361816657904233850711837826724976507876061043779177845550646612327663283839673950405472039432309339110648301347672597370034346896334064135099613033528785924976781865447506814375627647103020761819104412269362847848884053177378762432543285984193091289854949394023586451139393716370633759317698908834326333339634557238526607966375935960004339535161203546369910853171524516945554862019134684985868547068709986010719120259421723270478586992153989283736650948010894951434857207445141886625290777550599700738909777829412358941157674866550887608199469723859806486384262574182811908513849905040209776132986875560077482527819093584150609347560543627976011009349905362597935257895287412087233810106693141683912683308201407373637490835862452911394718399632093441745957478079754723679119343225931665785380948807114301164623717144211285944154289093317610519159281033583963015810556464180819928345256832572962092994993950655913067768151113345566231088759030148164710287289335782462899525700500822021472132532771242198844498579102733028256523810973405350508253183399668290339844052874956694050028526577775764032598023622192221226501334606994447870277619118208162133562541678250491647671042612088902952091163094215645902682086024337098910110924115381788866213335441814530141911754446270296630426675020592640816713810936850961517475097707179713115856513919631998747192606018612535436009162735908155778165736923393260822217415131632296810207091918499370220533980666393617256227781299273913099896641458499949922475383775878771011361417556799406151773618626138749998464514570482965286184387350245762317271666329142667987131139265167425606863654107927682419958167046956962340419295394058625063862272314570914511355913547470055072878835349142049813368019960713974852351833144227544851625677753419141348585495260230041111323576432417502998832485027071744397105338790364666312325483218209150368759120749671419554589928540715813531144241705501211397506662448276056294585639532800634727428900861538486753011006379264618338782036690340859441153926551954112364946638768223012718907190897165613382555036872673785444132692292217184974818914501886689736449912131900762592363447945036085692999163815947845753538999001633767986447907729462453017601有4888位,用时17.67188秒(这回可能结果对了,速度不快,不过也算是一种方法)
发一下这个程序供大家参考,欢迎老师探讨沟通和指导帮助!谢谢!
下面是代码:
Private Sub Command1_Click()
Dim a, B
Dim ja(), jb()
a = Trim(Text1): B = Trim(Text2): a3 = a: b3 = B
ts = Timer
If Len(a) < Len(B) Then
a = String(Len(B) - Len(a), "0") & a
B = B
Else
a = a
B = String(Len(a) - Len(B), "0") & B
End If
x = Len(a) \ 300: Y = x + 2: x11 = x
sb = Y * 2
a = String(Val(sb * 300 - Len(a)), "0") & a
B = String(Val(sb * 300 - Len(B)), "0") & B
x = sb / 2
ReDim ja(0 To x - 1): ReDim jb(-x + 1 To x - 1)
For I = 0 To x - 1
ja(I) = Mid(a, Len(a) - 600 * I - 599, 600)
jb(I) = Mid(B, Len(B) - 600 * I - 599, 600)
'倒序输出,减少加法计算量,提高速度
Next
For i0 = -1 To -x + 1 Step -1
jb(i0) = String(600, "0")
Next
jw2 = 0: jw3 = 0
For i1 = 0 To x - 1
d3 = ""
For i2 = 0 To x - 1
a1 = Mid(ja(i2), 1, 300): a2 = Mid(ja(i2), 301, 300)
B1 = Mid(jb(i1 - i2), 1, 300): b2 = Mid(jb(i1 - i2), 301, 300)
C1 = MbC(Trim(a1), Trim(B1)): C2 = MbC(Trim(a2), Trim(b2))
c3 = MPC1(Trim(a1), Trim(a2)): c4 = MPC1(Trim(B1), Trim(b2))
c5 = MbC(Trim(c3), Trim(c4))
D1 = MPC(Trim(c5), Trim(C1)): D1 = MPC(Trim(D1), Trim(C2))
D2 = MPC1(Trim(C1) & String(600, "0"), Trim(D1) & String(300, "0"))
D2 = MPC1(Trim(D2), Trim(C2))
d3 = MPC1(Trim(D2), Trim(d3))
Print qqdl(Trim(d3))
Next
d3 = MPC1(Trim(d3), Trim(jw3))
d5 = d3
If Len(d3) <= 600 Then
jw3 = 0
Else
jw3 = Left(d3, Len(d3) - 600)
End If
d3 = Right(d3, 600)
d4 = Trim(d3) & Trim(d4)
Next
d4 = Trim(jw3) & Trim(d4)
d4 = qqdl(Trim(d4))
d10 = Mid(d4, 1, 88)
Text3 = d4 & "有" & Len(d4) & "位,用时" & Timer - ts & "秒"
End Sub
Private Sub Command2_Click()
Text1 = ""
Text2 = ""
Text3 = ""
End Sub
Public Function MbC(D1 As String, D2 As String) As String
Dim x, Y ';两数长度
x = Len(D1) \ 4: Y = Len(D2) \ 4
d3 = String(4 * x + 4 - Len(D1), "0") & D1
d4 = String(4 * Y + 4 - Len(D2), "0") & D2
x = x + 1: Y = Y + 1
Dim a() As String
ReDim a(4 To 4 * x + 4 * Y, 4 To 4 * Y)
Dim I, J, C1, C2, CJ, JW, s, t
For J = 4 * Y To 4 Step -4 ';D2
JW = 0 ';进位清0
C2 = Mid(d4, J - 3, 4) ';每位数
For I = 4 * x To 4 Step -4 ';D1
C1 = Mid(d3, I - 3, 4) ';每位数
CJ = Val(C1) * Val(C2) + JW ';计算乘积
c = I + J: r = 4 * Y + 4 - J
a(c, r) = String(4 - Len(CJ Mod 10000), "0") & CJ Mod 10000 ';本位
JW = CJ \ 10000 ';进位
Next
a(c - 4, r) = JW
Next
Dim B() As String
ReDim B(1 To x + Y)
JW = 0
For s = x + Y To 1 Step -1
Bit = JW
For t = 1 To Y
Bit = Bit + Val(a(4 * s, 4 * t))
Next
B(s) = String(4 - Len(Bit Mod 10000), "0") & Bit Mod 10000
JW = Bit \ 10000
Next
If B(1) > 0 Then
MbC = Val(Left(MbC, 5)) & Mid(MbC, 6) & B(1)
Else
MbC = Val(Left(MbC, 5)) & Mid(MbC, 6)
End If
For s = 2 To x + Y
MbC = Val(Left(MbC, 5)) & Mid(MbC, 6) & B(s)
Next
End Function
'该程序已经做了修改,是模仿手工计算的程序,采用4位一组,在位数少于5000的时候,此程序居然比快速傅里叶变换的乘法还快!
Public Function MPC1(D1 As String, D2 As String) As String 'jiafa
Dim x, Y, JW '两数长度
If qqdl(D1) = "0" Then
MPC1 = D2
ElseIf qqdl(D2) = "0" Then
MPC1 = D1
Else
If Len(D1) >= Len(D2) Then
d4 = String(Len(D1) - Len(D2), "0") & D2
d3 = D1
Else
d4 = D2
d3 = String(Len(D2) - Len(D1), "0") & D1
End If
x = Len(d3) \ 8: Y = Len(d4) \ 8
If 8 * x < Len(d3) Then
d3 = String(8 * x + 8 - Len(d3), "0") & d3
d4 = String(8 * Y + 8 - Len(d4), "0") & d4
x = x + 1: Y = Y + 1
Else
x = x: Y = Y
d3 = d3: d4 = d4
End If
Dim a() As String, B1() As String, C1() As String, E1() As String
ReDim a(1 To x)
ReDim B1(1 To Y)
ReDim C1(1 To x)
ReDim E1(1 To x)
Dim I, J, C2, CJ
For J = Y To 1 Step -1 'D2
JW = 0 '进位清0
B1(J) = Mid$(d4, J * 8 - 7, 8) '每位数
For I = x To 1 Step -1 'D1
a(I) = Mid$(d3, I * 8 - 7, 8) '每位数
C1(I) = Val(a(I)) + Val(B1(I)) + Val(JW) '计算jia
If Len(C1(I)) < 8 Then
C1(I) = String(8 - Len(C1(I)), "0") & C1(I)
Else
C1(I) = C1(I)
End If
JW = Left(C1(I), Len(C1(I)) - 8)
E1(I) = Right(C1(I), 8)
Next
Next
For r = 1 To x
If JW = 0 Then
MPC1 = MPC1 & E1(r)
Else
jc = jc & E1(r)
MPC1 = JW & jc
End If
Next
MPC1 = qqdl(Trim(MPC1))
End If
End Function
Public Function MPC(D1 As String, D2 As String) As String ';jianfaqi
Dim x, Y ';两数长度
If Len(D1) >= Len(D2) Then
d4 = String(Len(D1) - Len(D2), "0") & D2
d3 = D1
Else
d4 = D2
d3 = String(Len(D2) - Len(D1), "0") & D1
End If
x = Len(d3): Y = Len(d4)
Dim a() As Integer, B1() As Integer, C1() As Integer, E1() As Integer
ReDim a(1 To x)
ReDim B1(1 To Y)
ReDim C1(1 To x)
ReDim E1(1 To x)
Dim I, J, C2, CJ, JW
For J = Y To 1 Step -1 ';D2
JW = 1 ';yu jie weichuzhi
B1(J) = Mid(d4, J, 1) ';每位数
For I = x To 1 Step -1 ';D1
a(I) = Mid(d3, I, 1) ';每位数
C1(I) = 10 + a(I) - B1(I) - 1 + JW ';计算jia
JW = C1(I) \ 10
E1(I) = C1(I) Mod 10
Next
Next
For r = 1 To x
MPC = MPC & E1(r)
For I = 1 To Len(MPC)
If Not Mid(MPC, I, 1) = "0" Then
Exit For
End If
Next
strtmp = Mid(MPC, I)
If Len(strtmp) = 0 Then
MPC = "0"
Else
MPC = strtmp
End If
Next
End Function
Private Function qqdl(sa As String) As String
For I = 1 To Len(sa)
If Not Mid(sa, I, 1) = "0" Then
Exit For
End If
Next
strtmp = Mid(sa, I)
If Len(strtmp) = 0 Then
qqdl = "0"
Else
qqdl = strtmp
End If
End Function