Fibonacci sequence generator overflow

Exceladd1ct

Board Regular
Joined
Feb 10, 2019
Messages
76
Hello,

I wrote a piece of code that would generate fibonacci sequence, but there is no data type that can hold such big numbers, is there a workaround to make the code contiune to do the math with big numbers?

Thanks.

VBA Code:
Option Explicit

Sub Fibonacci()
    Dim i As LongPtr, k As LongPtr
    i = 0
    k = 1
    
    Debug.Print i
    Debug.Print k
    
    Dim x As LongPtr, q As LongPtr, j As LongPtr, a As Long
    
    x = i + k 'x = 0+1
    q = x + k 'q = 1+1
    j = x + q 'j = 1+2
    
    Debug.Print k
    Debug.Print j
        For a = 1 To 1000
        x = q
        q = j
        j = x + q
        Debug.Print j
    Next
    
End Sub
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Here is a function that will generate the Nth Fibonacci number where N has no practical limit... the limitation is on the outputted number which cannot be more than 2,147,483,647 digits long (the maximum length of a text string).
VBA Code:
Function Fibonacci(ByVal N As Long) As String
  Dim f(1 To 3, 1 To 32767) As Byte
  Dim i As Long, loc As Long
  Dim addIx1 As Byte, addIx2 As Byte, sumIx As Byte
  Dim Carry As Byte, MaxLen As Long, nextDigit As Byte
  f(1, 1) = 1
  f(2, 1) = 1
  addIx1 = 1
  addIx2 = 2
  sumIx = 3
  MaxLen = 1
  For i = 3 To N
    Carry = 0
    loc = 1
    Do
      nextDigit = f(addIx1, loc) + f(addIx2, loc) + Carry
      If loc > MaxLen And nextDigit = 0 Then Exit Do
      f(sumIx, loc) = nextDigit Mod 10
      Carry = nextDigit \ 10
      loc = loc + 1
    Loop
    MaxLen = loc - 1
    addIx1 = (addIx1 Mod 3) + 1
    addIx2 = (addIx2 Mod 3) + 1
    sumIx = (sumIx Mod 3) + 1
  Next i
  For i = 1 To MaxLen
      Fibonacci = f(addIx2, i) & Fibonacci
  Next i
End Function
To use it the way you want, simply create a subroutine that will iterate from 1 to what ever Fibonacci number you want. For example, this one which will list the first 100 Fibonacci numbers to the Immediate Window...
VBA Code:
Sub ListFibonacciNumbers()
  Dim N As Long
  For N = 1 To 100
    Debug.Print "Fibonacci(" & N & ") = " & Fibonacci(N)
  Next
End Sub

Note: The Fibonacci function above was written by Eric W and uses Byte arrays to speed up a function I posted awhile ago which used much slower text string manipulations.
 
Last edited:
Upvote 0
Solution

Forum statistics

Threads
1,223,903
Messages
6,175,287
Members
452,631
Latest member
a_potato

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top