Calculations Needing Big Integers....
#5
Using arrays and carries is quite quick... Might be even quicker using LONG integers in the array.
There is also some problems with type conversions.
Does INTEGER to LONG conversion slow things down?
Anyway it is much quicker than using strings.
Code: (Select All)
Cls
Dim a(30000) As Integer, b(30000) As Integer
DefLng F-I
DefInt J-N
Input "input the fibonacci number to calculate"; fmax
tt = Timer
b(1) = 1: n = 1
For i = 1 To (fmax + 1) \ 2
    For j = 1 To n
        b(j) = a(j) + b(j) + jc
        If b(j) > 9999 Then b(j) = b(j) - 10000: jc = 1 Else jc = 0
    Next j
    If jc = 1 Then n = n + 1: b(n) = 1: jc = 0
    For j = 1 To n
        a(j) = a(j) + b(j) + jc
        If a(j) > 9999 Then a(j) = a(j) - 10000: jc = 1 Else jc = 0
    Next j
    If jc = 1 Then n = n + 1: a(n) = 1: jc = 0
Next i
For j = n To 1 Step -1
    If fmax Mod 2 = 0 Then
        If j = n Then t$ = LTrim$(Str$(a(j))) Else t$ = Right$("0000" + LTrim$(Str$(a(j))), 4)
        Print Using "&"; t$;
    End If
    If fmax Mod 2 = 1 Then
        If j = n Then t$ = LTrim$(Str$(b(j))) Else t$ = Right$("0000" + LTrim$(Str$(b(j))), 4)
        Print Using "&"; t$;
    End If
Next j
Print: Print "number of digits = ";
If fmax Mod 2 = 1 Then Print (n - 1) * 4 + Len(LTrim$(Str$(a(n))))
If fmax Mod 2 = 0 Then Print (n - 1) * 4 + Len(LTrim$(Str$(b(n))))
Print "TIME TAKEN= "; Timer - tt; "SECONDS"
End
Reply


Messages In This Thread
RE: Calculations Needing Big Integers.... - by david_uwi - 07-09-2023, 07:14 AM



Users browsing this thread: 4 Guest(s)