Fibonacci sequence help in VBA!!!

bglanton

New Member
Joined
Oct 3, 2014
Messages
10
  1. Ask the user to enter a number.
  2. Generate the Fibonacci sequence from 0 to that number.
 
this is very helpful, but is there a way to display the entire sequence up to the n?
Here is my previously posted Sub modified to output the list to a new sheet that the code adds automatically (and names with the Fibonacci Number the list on it goes up to)...
Code:
Sub Fibonacci()
  Dim X As Long, Z As Long, N As Long, Carry As Long, PositionSum As Long
  Dim N_minus_0 As String, N_minus_1 As String, N_minus_2 As String, FN As Variant
  N = Format(Application.InputBox("Please enter which Fibonacci Number you want to calculate...", Type:=1), "0")
  ReDim FN(1 To N, 1 To 2)
  If N = 1 Then
    FN(1, 1) = "'1"
    FN(1, 2) = "'1"
  ElseIf N = 2 Then
    FN(1, 1) = "'1"
    FN(1, 2) = "'1"
    FN(2, 1) = "'2"
    FN(2, 2) = "'1"
  ElseIf N > 2 Then
    N_minus_1 = "1"
    N_minus_2 = "1"
    FN(1, 1) = "'1"
    FN(1, 2) = "'1"
    FN(2, 1) = "'2"
    FN(2, 2) = "'1"
    For X = 3 To N
      Carry = 0
      N_minus_0 = Space$(Len(N_minus_1))
      If Len(N_minus_1) > Len(N_minus_2) Then N_minus_2 = "0" & N_minus_2
      For Z = Len(N_minus_1) To 1 Step -1
        PositionSum = Val(Mid$(N_minus_1, Z, 1)) + Val(Mid$(N_minus_2, Z, 1)) + Carry
        Mid$(N_minus_0, Z, 1) = Right$(CStr(PositionSum), 1)
        Carry = IIf(PositionSum < 10, 0, 1)
      Next
      If Carry Then N_minus_0 = "1" & N_minus_0
      FN(X, 1) = X
      FN(X, 2) = "'" & N_minus_0
      N_minus_2 = N_minus_1
      N_minus_1 = N_minus_0
    Next
    Sheets.Add after:=Sheets(Sheets.Count)
    ActiveSheet.Name = "Fibonacci(" & N & ")"
    Range("A1:B" & UBound(FN)) = FN
    Columns("A:B").AutoFit
  Else
    MsgBox "The number you enter must be greater than zero!"
  End If
End Sub
The above code is an "infinite" precision calculator which prints a list of the full Nth Fibonacci numbers up to virtually any maximum Fibonacci Number, limited only to the maximum number of digits that a string can hold. Be aware, however, this routine slows down as the inputted number gets larger; for example, my fairly fast computer and it took 7.8 seconds for it to calculate the 2090 digits for the number 9999 and 12.6 minutes to calculate the 20,899 digits for the Fibonacci Number 99999, so if you plan to print out lists for larger numbers, be forewarned it could take a considerable amount of time to do so.
 
Upvote 0

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
hi, this is a version of Fibonacci with recursive function.
Hope it helps.

Sub main_fibonacci()
Dim x As Integer
x = 11
Debug.Print fibo(x)

End Sub

Function fibo(n As Integer) As Integer

If n = 0 Then
fibo = 0 ' Stopping conditions
ElseIf n = 1 Then
fibo = 1
Else
fibo = fibo(n - 1) + fibo(n - 2)
End If

End Function
 
Upvote 0

Forum statistics

Threads
1,214,978
Messages
6,122,549
Members
449,089
Latest member
davidcom

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