Calculation of large numbers (5)

zhaozj2021-02-16  54

It's hard to imagine that as long as you change a few code, you can significantly improve the efficiency of the implementation. In the first few articles I wrote several large-scale algorithms (http://www.9cbs.net/develop/read_article.asp?id) = 28306, http://www.9cbs.net/develop/read_article.asp? Id = 28308, http://www.9cbs.net/develop/read_article.asp? ID = 28432), the efficiency is relatively low. Today, I saw the code of Homezj (http://www.9cbs.net/develop/Article/28/28584.shtm), which was inspired, optimized as follows:

Calculation is performed on each four:

Sub CalcFactorial (Byval Num As Long, Optional Byref Factorial AS String)

DIM Numlen As Long, Last As Long, I As Long, J AS Long, Temp as Long

Dim Result () As long, s () as string, stime as doubleumlen = 1stime = Timerredim Result (1 to Numlen) Result (1) = 1i = 0do while i

Factorial = JOIN (S, ") Debug.print Num &"!: Time "; Timer - Stime &" Second, Results "& Len (Factorial) &" Bit, the top 5 is "& Left (Factorial, 5) 'Debug.print Factorialerase SeraSe Resulten Sub

Private sub urns1_click () for i = 1 to 20calcfactorial i * 1000NEXTEND SUB

Output result: '1000! : When used, 7.82500000059372E-02 seconds, the result is 2568 bit, the top 5 is 40238' 2000! : Time. 39149999997905 seconds, 5736 bits, the top 5 is 33162'3000! : When used. 968875000005937 seconds, 9131, the top 5 is 41493'4000! : When used, 1.7817499999464 seconds, the result 12674 bit, the top 5 is 18288'5000! : 2.84412500000000326 seconds, 16326 bits, the top 5 is 42285'6000! : When used, 4.20387500000000652 seconds, 20066 bits, the top 5 is 26839'7000! : Time 5.84387500000000594 seconds, the result is 23,878 bits, the top 5 is 88420'8000! : 7.75012500000000594 seconds, the result 27753 bit, the top 5 is 51841'9000! : 9.98512500000000652 seconds, the result 31682 bit, the top 5 is 80995' 10000! : 12.51600000000333 seconds, 35660 bits, the top 5 is 28462'11000! : When using 15.2818750000006 seconds, 39681, the top 5 is 31624'12000! : When used 18.4063750000000059 seconds, the result 43742 bit, the top 5 is 12018'13000! : Use 21.7975000000006 seconds, 47838 bits, the top 5 is 77871'14000! : Time 25.5320000000065 seconds, 51969 bits, the top 5 is 13864'15000! : 29.5627499999973 seconds, the result 56130, the top 5 is 27465'16000! : 33.90625 seconds, the result is 60320, the top 5 is 5118717000! : 38.5786249999946 seconds, 64538 bits, the top 5 is 1379718000! : When used, 43.5477499999979 seconds, the result is 68781 bit, the top 5 is 13525'19000! : Use 48.84375 seconds, the result is 73048 bit, the top 5 is 18269' 20000! : 54.4375 seconds, the result is 7,7338 bits, the top 5 is 18192 <2> Calculated every five digits:

Sub CalcFactorial (Byval Num As Long, Optional Byref Factorial AS String)

DIM Numlen As Long, Last As Long, I As Long, J AS Long, Temp as Long

Dim Result () AS Long, S () AS String, Stime As Double

Numlen = 1stime = Timerredim Result (1 to Numlen) Result (1) = 1i = 0do while i

Private sub urns1_click () for i = 1 to 20calcfactorial i * 1000NEXTEND SUB

Output results:

1000! : When used, 6.26250000059372E-02 seconds, 2568 bits, the top 5 is 402382000! : Time. 297124999997322 seconds, 5736 bits, the top 5 is 331623000! : When used. 782000000006519 seconds, the result is 9131 bit, the top 5 is 414934000! : 1.421875 seconds, 12674 bits, the top 5 is 182885000! : 2.26612499999464 seconds, 16326 bits, the top 5 is 422856000! : 3.79700000000594 seconds, 20066, the top 5 is 268397000! : 4.65625 seconds, the result is 23,878 bits, the top 5 is 884208000! : When used, 6.2189999999732 seconds, the result 27753 bit, the top 5 is 518419000! : When used 8.375 seconds, the result is 31682 bits, the top 5 is 8099510000! : 9.98512500000000652 seconds, 35660 bits, the top 5 is 2846211000! : When used, 12.2351250000000065 seconds, 39681, the top 5 is 3162412000! : When used, 14.6882500000065 seconds, the result 43742 bit, the top 5 is 1201813000! : When used, 17.4696249999979 seconds, the result 47838 bit, the top 5 is 7787114000! : That 20.4071249999979 seconds, 51969 bits, the top 5 is 1386415000! : 23.625 seconds, the result is 56,130, the top 5 is 2746516000! : When used, 27.0783749999973 seconds, 60320 bits, the top 5 is 5118717000! : It is 30.875 seconds, the result is 64538 bit, the top 5 is 1379718000! : 34.8131250000006 seconds, 68781, the top 5 is 1352519000! : 39.0320000000065 seconds, 73048 bits, the top 5 is 1826920000! : When used at 43.5783749999973 seconds, the result is 77338 bits, the top 5 is 18192, it can be seen that the calculation is higher in five five digits. But one thing must be clear, the end of the "0" participation operations areted to waste time and memory, so shortening the runtime still has a considerable space.

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

New Post(0)