Calculation of large numbers (1)

zhaozj2021-02-16  52

The integer n step bypass 1 * 2 * 3 * ... * (n-1) * n value, when n = 171, the computer generally errors ("overflow"), this paper uses a string to simulate digital multiplication operation, Calculate 10000! become possible:

Function Multi (byval x as string, byval y as string) AS String 'Multi of Two huge Hexnum (two large amounts) Dim Result As Variantdim XL As Long, YL As Long, Temp as long, i as longXL = LEN (Trim (x)) YL = len (Trim (Y)) Redim Result (1 to XL YL) for i = 1 to XLFOR TEMP = 1 to YLRESULT (i TEMP) = Result (i Temp) VAL MID (X, I, 1)) * VAL (MID (Y, Temp, 1)) NextNext

For i = xl yl to 2 step -1temp = result (i) / 10Result (i) = Result (i) MOD 10RESULT (i - 1) = Result (i - 1) TEMPNext

If Result (1) = "0" Then Result (1) = "" "" Multi = Join (Result, "") ERASE Result

END FUNCTION

Private submmand1_click () 'saves time, calculated to 1000! For i = 1 to 9calcfactorial i * 100Nextend Sub

Sub CalcFactorial (byval n as integer) DIM A () AS String, I as long, Stimer As Doubleredim A (1 to n) a (1) = 1stimer = Timerfor i = 2 to NA (i) = Multi (A (i - 1), i) Nextdebug.print N & "!: Time"; Timer - Stimer & "Second, Results" & Len (A (N)) & "Bit" Debug.Print A (n) End Sub

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

New Post(0)