does any one have any code for solving the fibonacci series using recursion in VB.
Many thanks in Advance
Printable View
does any one have any code for solving the fibonacci series using recursion in VB.
Many thanks in Advance
Look here. Use the math forum for that stuff.http://www.vbforums.com/showthread.p...onacci+numbers
search for beachbum fibonacci and you will have a recursive function that he had did.
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
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
StuartVB Code:
Private Sub Command1_Click() Debug.Print FibNum(CLng(Text1.Text)) Debug.Print FibCalc(CLng(Text1.Text)) End Sub 'METHOD 1. EQUATION '------------------------------------------------------------ Private Function FibNum(Position As Long) As Double Dim FirstV As Double Dim SecondV As Double FirstV = (1 + Sqr(5)) / 2 SecondV = (1 - Sqr(5)) / 2 FibNum = (FirstV ^ Position - SecondV ^ Position) / Sqr(5) End Function 'METHOD 2. LOOPING '------------------------------------------------------------ Private Function FibCalc(Position As Long) As Double Dim FirstV As Double Dim SecondV As Double Dim lCounter As Long FirstV = 1 SecondV = 1 If Position > 2 Then For lCounter = 2 To Position - 1 FibCalc = FirstV + SecondV FirstV = SecondV SecondV = FibCalc Next Else FibCalc = 1 End If End Function