Results 1 to 5 of 5

Thread: fibonacci series in vb

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    fibonacci series in vb

    does any one have any code for solving the fibonacci series using recursion in VB.

    Many thanks in Advance
    steve

  2. #2
    Frenzied Member nishantp's Avatar
    Join Date
    Jan 2001
    Location
    Where you least expect me to be
    Posts
    1,375
    Look here. Use the math forum for that stuff.http://www.vbforums.com/showthread.p...onacci+numbers
    You just proved that sig advertisements work.

  3. #3
    DaoK
    Guest
    search for beachbum fibonacci and you will have a recursive function that he had did.

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331
    This is the code l am using, can anyone tell me why this does not work.

    Many thanks in advance

    Private Sub CmdFindNumber_Click()
    Dim number As Integer
    Dim answer As Integer

    number = InputBox("Please enter a number to find", "Find Fib Number")
    LblNumber.Caption = number
    answer = FibNumber(number)
    LblAnswer.Caption = answer

    End Sub

    Private Function FibNumber(number As Integer) As Integer

    If (number > 2) Then
    (FibNumber(number - 2) + FibNumber(number - 1))'should call itself
    End If

    End Function
    steve

  5. #5
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Hi steve
    You dont need recursion for the Fibonacci sequence. Here are two methods for providing the n-th number in the sequence. NB if u want to find all numbers up to the n-th sequence then u would need to use arrays... post again if u need this and i will code for you.
    regards
    Stuart
    VB Code:
    1. Private Sub Command1_Click()
    2.     Debug.Print FibNum(CLng(Text1.Text))
    3.     Debug.Print FibCalc(CLng(Text1.Text))
    4. End Sub
    5.  
    6. 'METHOD 1. EQUATION
    7. '------------------------------------------------------------
    8. Private Function FibNum(Position As Long) As Double
    9.     Dim FirstV As Double
    10.     Dim SecondV As Double
    11.    
    12.     FirstV = (1 + Sqr(5)) / 2
    13.     SecondV = (1 - Sqr(5)) / 2
    14.     FibNum = (FirstV ^ Position - SecondV ^ Position) / Sqr(5)
    15. End Function
    16.  
    17. 'METHOD 2. LOOPING
    18. '------------------------------------------------------------
    19. Private Function FibCalc(Position As Long) As Double
    20.     Dim FirstV As Double
    21.     Dim SecondV As Double
    22.     Dim lCounter As Long
    23.    
    24.     FirstV = 1
    25.     SecondV = 1
    26.     If Position > 2 Then
    27.         For lCounter = 2 To Position - 1
    28.             FibCalc = FirstV + SecondV
    29.             FirstV = SecondV
    30.             SecondV = FibCalc
    31.         Next
    32.     Else
    33.         FibCalc = 1
    34.     End If
    35. End Function
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width