Question Whats Wrong with this Fibonacci Application?

Haxaro

Well-known member
Joined
Mar 13, 2009
Messages
105
Programming Experience
1-3
For my application, i need the user to input a number, and the application will work out the highest possible Fibonacci number below the user inputed number.

At the moment, my application is providing non-Fibonacci numbers, which is a problem.

Here is the code:
VB.NET:
Public Class Form1

    Dim Input = Input1.Text
    Dim output = Output1.Text

    Private Sub Main(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Work.Click

        ' Declarations
        Dim FirstNum = 1 'Declare
        Dim OverFlow = 1 'Declare
        Dim BackUp = 1 'Declare
        Dim PreResult = 1 'Declare
        Dim Result = 1 'Declare
        Dim FinalResult = 1 'Declare


        Do Until Result > Input() ' Loop until the result is greater than the user inputed text
            FirstNum = BackUp ' Prepare for next loop
            PreResult = Result + FirstNum ' Add X + Y = Z
            BackUp = PreResult ' Prepare for next loop
            Result = PreResult ' Prepare for next loop
        Loop

        If Result > Input() Then ' When the result is greater than the user inputed text, run:
            FinalResult = Result - BackUp ' Go back one Fibonacci number
            output = FinalResult ' Output the final Number lower than the user inputed text
        End If

    End Sub
End Class

Any help would be greatly appreciated. Thanks!
Haxaro
 
1. Option Strict should be on.

2. Why are you using Textboxes, when a NumericUpDown control would be far more suited to this?

3.
VB.NET:
        Dim DesiredValue As Integer = 372

        Dim FirstNum As Integer = 0
        Dim SecondNum As Integer = 1
        Dim Result As Integer = 0

        While FirstNum + SecondNum < DesiredValue
            Result = FirstNum + SecondNum
            FirstNum = SecondNum
            SecondNum = Result
        End While

        Result = SecondNum
 
Great Help

Thanks. That was a great help, but i dont know too much about option strict, so im not sure how i am ment to convert a string (text box) to a integer in option strict...

VB.NET:
Option Strict On
Public Class Form1

    Private Sub Main(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim Value As Integer = ***Input.Text***
        Dim FirstNum As Integer = 1
        Dim SecondNum As Integer = 1
        Dim Result As Integer = 1

        Do Until FirstNum + SecondNum < Value
            Result = FirstNum + SecondNum
            FirstNum = SecondNum
            SecondNum = Result
        Loop

        Result = SecondNum
        ***OutPut.Text = Result***
    End Sub
End Class

Help with the *** sections would be appreciated. Thanks!
 
Didn't Work

I just got it working, but it still doesnt get the correct answer..

VB.NET:
Option Strict On
Public Class Form1

    Private Sub Main(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Work.Click

        Dim Value As Integer = CInt(Input1.Text)
        Dim FirstNum As Integer = 1
        Dim SecondNum As Integer = 1
        Dim Result As Integer = 0
        Dim Old As Integer = 0
  
        Do Until FirstNum + SecondNum > Value
            Result = FirstNum + SecondNum
            Old = FirstNum
            FirstNum = SecondNum
            SecondNum = Result
        Loop


        Result = Result - Old
        Output1.Text = CStr(Result)
    End Sub
End Class
 
VB.NET:
        Result = SecondNum
        Output1.Text = Result.ToString

and, for reference, Fibonacci starts at 0 and 1, not 1 and 1, so your code should be:-

VB.NET:
        Dim FirstNum As Integer = 0
        Dim SecondNum As Integer = 1
 
Well either way, it still isnt working. It comes up with a fibonacci number, but it isnt the highest... any reasons why - or how i can fix this?

VB.NET:
Option Strict On
Public Class Form1

    Private Sub Main(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Work.Click

        Dim Value As Integer = CInt(Input1.Text)
        Dim FirstNum As Integer = 0
        Dim SecondNum As Integer = 1
        Dim Result As Integer = 1
        Dim Real As Integer = 1
  
        Do Until FirstNum + SecondNum >= Value
            Result = FirstNum + SecondNum
            Real = SecondNum
            FirstNum = SecondNum
            SecondNum = Result
        Loop


        Output1.Text = CStr(Real)
    End Sub
End Class
 
How about giving an example of what values you are trying, and what result it is giving? I didnt pass my course in mind reading :)
 
Fibonacci sequence

Public Function Fibonacci(ByVal mynum As System.Int64) As System.Int64
If mynum = 0 Then
Return 0
ElseIf mynum = 1 Then
Return 1
Else
Return Fibonacci(mynum - 1) + Fibonacci(mynum - 2)
End If
End Function

Public Function closestFibonnacci(ByVal mynum As System.Int64) As System.Int64
Dim index, result As System.Int64
Do
result = Fibonacci(index)
index += 1
Loop Until result >= mynum

Do
result = Fibonacci(index)
index -= 1
Loop Until result <= mynum
Return result
End Function

' this code works. Place a numeric updown control and a textbox on your form and display the Fibonacci number closest to the user input value using an instruction like:

TextBox1.Text= closestFibonacci(CLng(NumericUpDown1.value)).ToString

You will need the 'CLng' if Option Strict is on.
 
Back
Top