|
-
Jan 5th, 2002, 06:19 PM
#1
Thread Starter
Frenzied Member
fibonacci series in vb
does any one have any code for solving the fibonacci series using recursion in VB.
Many thanks in Advance
-
Jan 5th, 2002, 07:13 PM
#2
Frenzied Member
You just proved that sig advertisements work.
-
Jan 5th, 2002, 09:04 PM
#3
search for beachbum fibonacci and you will have a recursive function that he had did.
-
Jan 6th, 2002, 04:56 AM
#4
Thread Starter
Frenzied Member
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
-
Jan 6th, 2002, 06:17 AM
#5
PowerPoster
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:
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|