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




Reply With Quote