Calculation of large numbers (5)

zhaozj2021-02-17  50

For calculation (4) I always feel that there is no maximum potential for long-playing long, and it must be my algorithm and there is room for improvement. After further scrutinizing, I finally have breakthroughs, and the speed suddenly increased by 4 times! 10,000 on the same machine! Used 9.8 seconds, and boldly calculated a 20000 !,, ah, 43 seconds. Apparent code is posted, for your reference, can you still have it?

Private Function CaCl (Num As Long) AS STRINGDIM NUMLEN AS Long, Last As Long, X As Longdim I As Long, M As Long, N As Long, NL As Long, S0 As Stringdim Result () As long, StartTime As Single, s () as stringnumlen = 1startTime = Timerredim Result (1 to Numlen) NL = 9 - len (cstr (num)) 'According to the multiplication of the maximum number of two numbers, it will not exceed the principle of the sum of the sum of the lengths,' Let arrays The sum of the length of each element and the number of steps cannot exceed 9 to prevent overflow. IF NL <1 THEN NL = 1 'The minimum length is 1 bit, if it is true to such a large number, I am afraid no one will try ^ - ^ n = 10 ^ NL' cache for separating the large number of divisors, arrays The length of each element is NL, the number is 10 NL (1) = 1X = 1do while x <= Num Last = 0 for i = 1 To Numlen M = Result (i) * x Last 'array Each element is multiplied by the number of passes, plus the last carry of RESULT (I) = m mod n 'separated the large number LAST = m / n' Save the port number and waits to the next array element NEXT. If Last> 0 THEN M = LEN (CSTR (Last) / NL 1 'to add an array size to the upper limit of the array element, and separate the Redim Preserve Result (1 to Numlen M) for i = Press Length NL 1 TO M Result (Numlen I) = Last MOD N LAST = LAST / N Next Numlen = Ubound (Result) end if x = x 1Loopredim S (1 to Numlen) S0 = String (NL, "0") ' The array of lengths in the length of NL is to make up in front, otherwise the result is wrong for I = 1 to Numlens (i) = format (RESULT (Numlen 1 - i), S0) 'format supplement 0 each Array elements NEXTS (1) = Val (s (1)) IF S (1) = 0 THEN S (1) = "" 'The highest bit is to drop 0. Although the result is not affected, the number of bits will be wrong. Cacl = Join (s, ") Debug.print Num &"!: Time "; Timer - StartTime &" Second, Results "& Len (CACL) &" Bit "End Function

PRIVATE SUB Command1_Click () DIM i as long'cacl 20000for i = 1 to 9cacl i * 100NextFor i = 1 to 10cacl i * 1000NEXTEND SUB calculation results:

100! : Use time 0 seconds, result 158 ​​bits 200! : Use time 0 seconds, result 375 bits 300! : When used .015625 seconds, 615 bits 400! : Use it 0 seconds, the result 869 bits 500! : When used .015625 seconds, result 1135 bits 600! : When used .015625 seconds, 1409 bit 700! : Time .03125 seconds, result 1690 bits 800! : When used .03125 seconds, the result 1977 bit 900! : Time .0625 seconds, result 2270 bits 1000! : Time .078125 seconds, result 2568 bits 2000! : Time. 3125 seconds, result 5736 bit 3000! : When used. 75 seconds, the result 9131 bits 4000! : When used 1.390625 seconds, the result 12674 bits 5000! : When used 2.265625 seconds, the result 16326 bits 6000! : 3.3125 seconds, 20066 bit 7000! : Use 4.609375 seconds, the result 23878 bit 8000! : 6.125 seconds, the result 27753 bit 9000! : 7.9375 seconds, the result 31682 bits 10000! : 9.890625 seconds, 35660

This algorithm has a defect: the larger the number of steps, the more efficient, the more efficient, and when calculating the 8-digit step, it will drop to calculate (4) equivalent, the maximum number of multipliers still Can you exceed 2 billion. However, the length of the string may also overflow due to long overflow.

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

New Post(0)