大数运算(二)

来源:互联网 发布:韩信军事才能知乎 编辑:程序博客网 时间:2024/05/29 18:30
Author:水如烟 

这是首次测试。下面是测试代码。项目代码容后再列:

Public Class BigIntDemo

    
Private Delegate Sub AppendStringHandler(ByRef b As System.Text.StringBuilder, ByVal line As String)

    
Public Shared Sub Run()
        
Dim builder As New System.Text.StringBuilder
        
Dim w As New System.Diagnostics.Stopwatch
        
Dim AppendAction As AppendStringHandler = New AppendStringHandler(AddressOf AppendString)


        builder.AppendLine(
"这是大数加、乘运算的测试程序")
        builder.AppendLine(
"先按常规计算1000的阶乘")
        builder.AppendLine()

        
Dim aValue As New BigIntLibrary.StringValue("1")
        
Dim bValue As BigIntLibrary.StringValue

        
Dim a As New BigIntLibrary.BigInt
        
Dim b As New BigIntLibrary.BigInt

        w.Start()

        a.StringValue 
= aValue

        
For i As Integer = 1 To 1000
            bValue 
= New BigIntLibrary.StringValue(i.ToString)
            b.StringValue 
= bValue

            a.Multy(b)
        
Next

        w.Stop()

        builder.AppendLine(
String.Format("计算结束,用时:{0}。结果如下:", w.Elapsed))
        builder.AppendLine()

        AppendAction.Invoke(builder, a.StringValue.GetFullValue)

        builder.AppendLine(
"先把结果保存下来")
        builder.AppendLine()

        
Dim cValue As BigIntLibrary.StringValue = a.StringValue.Clone

        builder.AppendLine(
"下一步,再算,这次分三个区间[1,500],[501,800],[801,1000]。也是过程算法,暂不用线程")
        builder.AppendLine()


        w.Reset()

        w.Start()

        a.StringValue 
= aValue
        
For i As Integer = 1 To 500
            bValue 
= New BigIntLibrary.StringValue(i.ToString)
            b.StringValue 
= bValue

            a.Multy(b)
        
Next

        w.Stop()
        builder.AppendLine(
String.Format("计算区间[1,500]结束,已用时:{0},结果是:", w.Elapsed))
        builder.AppendLine()

        AppendAction.Invoke(builder, a.StringValue.GetFullValue)

        
Dim c1 As BigIntLibrary.StringValue = a.StringValue.Clone

        w.Start()

        a.StringValue 
= aValue
        
For i As Integer = 501 To 800
            bValue 
= New BigIntLibrary.StringValue(i.ToString)
            b.StringValue 
= bValue

            a.Multy(b)
        
Next

        w.Stop()
        builder.AppendLine(
String.Format("计算区间[501,800]结束,已用时:{0},结果是:", w.Elapsed))
        builder.AppendLine()

        AppendAction.Invoke(builder, a.StringValue.GetFullValue)

        
Dim c2 As BigIntLibrary.StringValue = a.StringValue.Clone


        w.Start()

        a.StringValue 
= aValue
        
For i As Integer = 801 To 1000
            bValue 
= New BigIntLibrary.StringValue(i.ToString)
            b.StringValue 
= bValue

            a.Multy(b)
        
Next

        w.Stop()

        builder.AppendLine(
String.Format("计算区间[801,1000]结束,已用时:{0},结果是:", w.Elapsed))
        builder.AppendLine()

        AppendAction.Invoke(builder, a.StringValue.GetFullValue)

        builder.AppendLine(
"现在将上面的三个结果相乘")
        builder.AppendLine()


        w.Start()

        b.StringValue 
= c1
        a.Multy(b)

        b.StringValue 
= c2
        a.Multy(b)

        w.Stop()

        builder.AppendLine(
String.Format("全部计算结束,总用时:{0},结果是:", w.Elapsed))
        builder.AppendLine()

        AppendAction.Invoke(builder, a.StringValue.GetFullValue)

        builder.AppendLine(
String.Format("检测两种方法计算结果是否相同,结果是:{0}", a.StringValue.Equals(cValue)))
        builder.AppendLine()


        builder.AppendLine(
"下次看看分拆运算是否可行,之后,再考虑多线程计算。")
        builder.AppendLine()


        builder.AppendLine(
"测试结束。")
        Console.WriteLine(builder.ToString)
    
End Sub



    
Private Shared Sub AppendString(ByRef b As System.Text.StringBuilder, ByVal line As String)
        b.AppendLine()
        
For Each s As String In SplitStringToArray(line, 100)
            b.AppendLine(s)
        
Next
        b.AppendLine()
    
End Sub

    
Private Shared Function SplitStringToArray(ByVal line As StringByVal digits As IntegerAs String()
        
Dim mLineLength As Integer = line.Length
        
Dim mCount As Integer = mLineLength  digits
        
If (mLineLength Mod digits) > 0 Then mCount += 1

        
Dim mResult(mCount - 1As String

        
Dim mCurrentIndex As Integer
        
For i As Integer = mCount - 1 To 0 Step -1
            mCurrentIndex 
= mLineLength - (mCount - i) * digits

            
If mCurrentIndex < 0 Then
                mResult(
0= line.Substring(0, mLineLength - (mCount - 1* digits)
            
Else
                mResult(i) 
= line.Substring(mCurrentIndex, digits)

            
End If

        
Next

        
Return mResult
    
End Function
End Class

 

输出结果是:

这是大数加、乘运算的测试程序
先按常规计算1000的阶乘

计算结束,用时:00:00:16.1344364。结果如下:


40238726007709377354370243392300398571937486421071463254379991042993
8512398629020592044208486969404800479988610197196058631666872994808558901323829669944590997424504087
0737599188236277271887325197795059509952761208749754624970436014182780946464962910563938874378864873
3711918104582578364784997701247663288983595573543251318532395846307555740911426241747434934755342864
6576611667797396668820291207379143853719588249808126867838374559731746136085379534524221586593201928
0908782973084313928444032812315586110369768013573042161687476096758713483120254785893207671691324484
2623613141250878020800026168315102734182797770478463586817016436502415369139828126481021309276124489
6359928705114964975419909342221566832572080821333186116811553615836546984046708975602900950537616475
8477284218896796462449451607653534081989013854424879849599533191017233555566021394503997362807501378
3761530712776192684903435262520001588853514733161170210396817592151090778801939317811419454525722386
5541461062892187960223838971476088506276862967146674697562911234082439208160153780889893964518263243
6716167621791689097799119037540312746222899880051954444142820121873617459926429565817466283029555702
9902432415318161721046583203678690611726015878352075151628422554026517048330422614397428693306169089
7968482590125458327168226458066526769958652682272807075781391858178889652208164348344825993266043367
6601769996128318607883861502794659551311565520360939881806121385586003014356945272242063446317974605
9468257310379008402443243846565724501440282188525247093519062092902313649327349756551395872055965422
8749774011413346962715422845862377387538230483865688976461927383814900140767310446640259899490222221
7659043399018860185665264850617997023561938970178600408118897299183110211712298459016419210688843871
2185564612496079872290851929681937238864261483965738229112312502418664935314397013742853192664987533
7218940694281434118520158014123344828015051399694290153483077644569099073152433278288269864602789864
3211390835062170950025973898635542771967428222487575867657523442202075736305694988250879689281627538
4886339690995982628095612145099487170124451646126037902930912088908694202851064018215439945715680594
1872748998094254742173582401063677404595741785160829230135358081840096996372524230560855903700624271
2434169090041536901059339838357779394109700277534720000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

先把结果保存下来

下一步,再算,这次分三个区间[1,500],[501,800],[801,1000]。也是过程算法,暂不用线程

计算区间[1,500]结束,已用时:00:00:03.7163808,结果是:


12201368259911100687012387854230469
2625357434280319284219241358838584537315388199760549644750220328186301361647714820358416337872207817
7200480785205159329285477907571939330603772960859086270429174547882424912726344305670173270769461062
8023104526442188787894657547771498634943677810376442740338273653974713864778784954384895955375379904
2324106127132698432774571554630997720278101456108118837370953101635632443298702956389662891165897476
9572087926928871281780070265174507768410719624390394322536422605234945850129918571501248706961568141
6253590566934238130088562492468915641267756544818865065938479517753608940057452389403357984763639449
0531306232374906644504882466507594673586207463792518420045936969298102226397195259719094521782333175
6934581508552332820762820023402626907898342451712006207714640979456116127629145951237229913340169552
3638509428855920187274337951730145863575708283557801587354327688886801203998823847021514676054454076
6353598417443048012893831389688163948746965881750450692636533817505547812864000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

计算区间[501,800]结束,已用时:00:00:05.5748886,结果是:


6319397914320503473398349966523765037454484
8529700476417292421729784872142360209160653092322125557657851577916757999684858549088785341091242924
2950326496727831612599574400971454732285467228536293535847095437500879097692217946084582952252573171
6281274985427835688505676597573292752912682520529347979620064036449726214606549083492023612286541133
8084415358203454456782887241593876245107490363513702561320082708759746680944552319389729191044200136
3908261115010556179420520901911423670819277974372574068552998402188091105646512860566199419632739927
6148627582812817251337719934673498478297225694410263719040187147822117742119716637666764710538195508
6464828303771013007889475681229537725561810780473751570895169323798708111957293870103718521155632972
6125469500935311001124864000000000000000000000000000000000000000000000000000000000000000000000000000

计算区间[801,1000]结束,已用时:00:00:06.4959913,结果是:


5218671792490631044317715323190148027733406067350406943382787853546276913659411647831410538
7030290844851582694051872976267534543644887479432199988330802073591596160470985881999638538818793585
9640654596335746881134780531452843909715448234514762462143856204913946601253808724104934316876333775
7716844581876482817979915849271601556399510803245660021952364076087218601540599674433273554897312911
0540005618969135791320323515498872646826064176507198312357091618478052693591011017474181708592801076
7101123823291935770762480197142805725028939936563200000000000000000000000000000000000000000000000000

现在将上面的三个结果相乘

全部计算结束,总用时:00:00:07.2554654,结果是:


40238726007709377354370243392300398571937486421071463254379991042993
8512398629020592044208486969404800479988610197196058631666872994808558901323829669944590997424504087
0737599188236277271887325197795059509952761208749754624970436014182780946464962910563938874378864873
3711918104582578364784997701247663288983595573543251318532395846307555740911426241747434934755342864
6576611667797396668820291207379143853719588249808126867838374559731746136085379534524221586593201928
0908782973084313928444032812315586110369768013573042161687476096758713483120254785893207671691324484
2623613141250878020800026168315102734182797770478463586817016436502415369139828126481021309276124489
6359928705114964975419909342221566832572080821333186116811553615836546984046708975602900950537616475
8477284218896796462449451607653534081989013854424879849599533191017233555566021394503997362807501378
3761530712776192684903435262520001588853514733161170210396817592151090778801939317811419454525722386
5541461062892187960223838971476088506276862967146674697562911234082439208160153780889893964518263243
6716167621791689097799119037540312746222899880051954444142820121873617459926429565817466283029555702
9902432415318161721046583203678690611726015878352075151628422554026517048330422614397428693306169089
7968482590125458327168226458066526769958652682272807075781391858178889652208164348344825993266043367
6601769996128318607883861502794659551311565520360939881806121385586003014356945272242063446317974605
9468257310379008402443243846565724501440282188525247093519062092902313649327349756551395872055965422
8749774011413346962715422845862377387538230483865688976461927383814900140767310446640259899490222221
7659043399018860185665264850617997023561938970178600408118897299183110211712298459016419210688843871
2185564612496079872290851929681937238864261483965738229112312502418664935314397013742853192664987533
7218940694281434118520158014123344828015051399694290153483077644569099073152433278288269864602789864
3211390835062170950025973898635542771967428222487575867657523442202075736305694988250879689281627538
4886339690995982628095612145099487170124451646126037902930912088908694202851064018215439945715680594
1872748998094254742173582401063677404595741785160829230135358081840096996372524230560855903700624271
2434169090041536901059339838357779394109700277534720000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

检测两种方法计算结果是否相同,结果是:True

下次看看分拆运算是否可行,之后,再考虑多线程计算。

测试结束。

 

原创粉丝点击