| Calculates Fibonacci numbers by using a recursive function. Good example of how a recursive function operates. Inputs: n - what fibonacci number you want to calculate. Assumes:Fibonacci means that the number is equal to the last two fibonacci numbers added together given the fibonacci of 1 is 0 and the fibonnacci of 2 is 1. Side Effects: It begins to get slow at 30 on my computer. If anyone knows a way to speed it up, please speak! Public Function Fibonacci(ByVal n As Long) As Long If n <= 1 Then Fibonacci = 0 ElseIf n = 2 Then Fibonacci = 1 Else ' return the two last fibonacci numbers Fibonacci = Fibonacci(n - 1) + Fibonacci(n - 2) End If End Function 'Example of how it's used. I know it's a bad example :P Private Sub Form_Load() Dim sInput As String sInput = Trim$(InputBox("Input a number...", "Fibonacci")) Do While sInput <> "-1" sInput = Trim$(InputBox("The Fibonacci of " & sInput & " is " & Fibonacci(CLng(sInput)) & "." & vbCrLf & vbCrLf & "Input a new number.-1 To quit.", "Fibonacci", sInput)) Loop End End Sub |
Fibonacci Numbers |
India web developer web development India | India web development company India ecommerce web developer