Calculation of large numbers (four)

zhaozj2021-02-17  50

Look at Northwolves's articles, the calculation of large numbers (3) http://dev.9cbs.net/Article/28/28432.shtm is very inspired, Lenovo has written a similar function before, and the method is almost, It is a dynamic array to increase the mode of saving. The efficiency is worse than the Northwolves brother, about 50% of the difference; at that time, it thought it was the fastest level algorithm, so see this article is very admired!

However, I always think that this algorithm has an optimized room, so I have re-made some new attempts, I don't want to be a big success, the efficiency is more than 5 times more than the calculation of the large number of steps (3). More than 5 times, and The algorithm is simple and amazing, so I don't dare to swallow, and I will have a sharing with everyone.

Option ExplicitPrivate Function cacl (num As Long) As StringDim numlen As Long, last As Long, x As LongDim i As Long, m As Long, n As LongDim result () As Long, starttime As Singlenumlen = 1starttime = TimerReDim result (1 To Numlen Result (1) = 1x = 1do while x <= Num Last = 0 for i = 1 to Numlen M = Result (i) * x Last Result (i) = m mod 10 last = m / 10 Next if LAST > 0 THEN N = LEN (CSTR (Last) Redim Preserve Result (1 to Numlen N) for i = 1 to n Result (Numlen i) = Last MOD 10 LAST = LAST / 10 Next Numlen = Ubound (Result) END IF X = X 1Loopredim S (1 to Numlen) for i = 1 to Numlens (i) = Result (Numlen 1 - i) NextCacl = JOIN (S, ") Debug.print Num &"!: ; Timer - StartTime & "Second, Results" & Numlen & "Bit" End Function

PRIVATE SUB Command1_Click () DIM I as Longfor I = 1 to 10cacl i * 100Next'For i = 1 to 10'cacl i * 1000'Nextend Sub

Output results: (My machine is running "calculation (3)", comparable to it, so it should be with Northwolves brother, the same grade machine)

100! : Use time 0 seconds, result 158 ​​bits 200! : When used 1.171875E-02 seconds, the result is 375 bits 300! : When used at 3.515625E-02 seconds, the result is 615 bits 400! : Use 4.296875e-02 seconds, 869 bits 500! : When used, 8.203125E-02 seconds, the result 1135 bits 600! : Time. 1054688 seconds, 1409 bit 700! : Time. 15625 seconds, result 1690 bits 800! : When used. 1992188 seconds, result 1977 bit 900! : When used. 2617188 seconds, result 2270 bits 1000! : Time. 3632813 seconds, result 2568 bits 2000! : 1.53125 seconds, the result 5736 bit 3000! : 3.75 seconds, the result 9131 bits 4000! : 7.171875 seconds, 12674 bits 5000! : 11.45313 seconds, the result 16326 bits 6000! : When used 16.41016 seconds, the result 20066 bit 7000! : 22.83984 seconds, the result 23878 bits 8000! : Use 30.375 seconds, the result 27753 bit 9000! : Use 41.75 seconds, result 31682 bits 10000! : That is 54.57813 seconds, the result is 65660 this time is slow, because I am tested 10,000! When it took only 47 seconds. I don't know if there will be a faster algorithm.

转载请注明原文地址:https://www.9cbs.com/read-29907.html

New Post(0)