Results 1 to 18 of 18

Thread: Fibonacci Help, Im stuck

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2013
    Posts
    31

    Fibonacci Help, Im stuck

    I am solving a problem that has to do with Fibonacci numbers but ive been stuck for the longest time trying to figure this out. Given a specified “distance” from the golden ratio, I have to determine how
    many Fibonacci numbers need to be generated in order to get this close.
    How do I store these values into a string variable?
    fib1 = 0
    fib2 = 1
    fib3 = fib1 + fib2

    Here is my the code I have so far for the whole question, im quite confused and lost

    Code:
    Private Sub calculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles calculateButton.Click
            'declare variables
            Dim input As Double
            Dim fib1 As Integer
            Dim fib2 As Integer
            Dim fib3 As Integer
            Dim ratio As Double
            Const GOLDEN_RATIO As Double = 1.618
    
            'assign user input
            input = Val(inputTextBox.Text)
    
            'assign fibonacci numbers
            fib1 = 0
            fib2 = 1
            fib3 = fib1 + fib2
            Dim display As String = "Fibonacci numbers generated:" & ControlChars.NewLine
    
            ratio = GOLDEN_RATIO - input
    
    
            Do
                fib2 = fib3
                fib3 = fib1 + fib2
                display &= ", " & fib3
            Loop Until ratio = GOLDEN_RATIO
    
    
            input = Math.Abs(GOLDEN_RATIO - ratio)
    
    
    
        End Sub
    End Class

  2. #2
    Hyperactive Member
    Join Date
    Oct 2004
    Posts
    259

    Re: Fibonacci Help, Im stuck

    what is the use of storing them as a string?
    just for display sake? or to tokenize them for later UN-Stringing them?
    (surprisingly, everything else i understood)
    ----------------------------------------------------

    Missing the days of GWBasic 1.1
    WaxyStudios.com

  3. #3

    Thread Starter
    Junior Member
    Join Date
    May 2013
    Posts
    31

    Re: Fibonacci Help, Im stuck

    I guess to send them as a display to the multiline textbox i will be using to show the fibonacci numbers that had to be used

  4. #4
    Hyperactive Member
    Join Date
    Oct 2004
    Posts
    259

    Re: Fibonacci Help, Im stuck

    You already have them stored as a string in "Display".

    i just don't know if you want ControlChars.NewLine or VBCRLF (vb carriage return line feed)

    textbox.text = display
    ----------------------------------------------------

    Missing the days of GWBasic 1.1
    WaxyStudios.com

  5. #5

    Thread Starter
    Junior Member
    Join Date
    May 2013
    Posts
    31

    Re: Fibonacci Help, Im stuck

    ah so i did do that right under the 'assign fibonacci numbers?
    I do want them as ControlChars.NewLine well at least I think I do. Im honestly having hell of a time with this, when i try to run it to see what happens it just crashes

  6. #6

    Thread Starter
    Junior Member
    Join Date
    May 2013
    Posts
    31

    Re: Fibonacci Help, Im stuck

    Ier t says that after doing fib2 = fib3
    fib3 = fib1 + fib2
    Now store the “new” fib3 before calculating the next number.
    2. You can store this information in a variable of String data type.

    Yes this is homework but im trying really hard and posting my own code to see whats wrong and how I can fix it. I believe this way is the right way to do it without mods frowning upon me? And also learning in the process

  7. #7
    Hyperactive Member
    Join Date
    Oct 2004
    Posts
    259

    Re: Fibonacci Help, Im stuck

    I got sidetracked by something in your code.

    fib2 = fib3
    fib3 = fib1 + fib2
    display &= ", " & fib3

    wouldn't this end up being 1 + 2 = 3 , 1 + 3 = 4, 1+4 = 5 ?

    shouldn't it be :
    1 + 2 = 3
    2 + 3 = 5
    3 + 5 = 8

    i might be wrong and just really tired.
    ----------------------------------------------------

    Missing the days of GWBasic 1.1
    WaxyStudios.com

  8. #8

    Thread Starter
    Junior Member
    Join Date
    May 2013
    Posts
    31

    Re: Fibonacci Help, Im stuck

    hmm that part i got straight from the question it confused me even more though so it might be wrong. So should I just do my loop with the fib1 = 0 fib2 = 1 fib3 = fib1 + fib2 or how would I do he loop to see how ay fibonacci numbers it would take to get the golden ratio that distance away? (from the user input)

  9. #9
    Hyperactive Member
    Join Date
    Oct 2004
    Posts
    259

    Re: Fibonacci Help, Im stuck

    .

    check this link

    fib1 = 0
    fib2 = 1
    fib3 = fib1 + fib2

    ....

    fib1 = fib2
    fib2 = fib3
    fib3 = fib1+fib2

    where does it crash?

    your output sounds like you just need a textbox and have that TextboxName.Text = display
    ----------------------------------------------------

    Missing the days of GWBasic 1.1
    WaxyStudios.com

  10. #10
    Hyperactive Member
    Join Date
    Oct 2004
    Posts
    259

    Re: Fibonacci Help, Im stuck

    found your crash

    Do
    fib2 = fib3
    fib3 = fib1 + fib2
    display &= ", " & fib3
    Loop Until ratio = GOLDEN_RATIO

    it will loop forever if your Fibonacci is wrong and you never hit a golden ratio. that's the crash.
    ----------------------------------------------------

    Missing the days of GWBasic 1.1
    WaxyStudios.com

  11. #11

    Thread Starter
    Junior Member
    Join Date
    May 2013
    Posts
    31

    Re: Fibonacci Help, Im stuck

    Ah ya i figured that's where the problem was but I wasnt quite sure how to fix that. I fell asleep at my computer like a good student lol
    Last edited by newvbusernoob; May 7th, 2013 at 09:21 AM.

  12. #12

    Thread Starter
    Junior Member
    Join Date
    May 2013
    Posts
    31

    Re: Fibonacci Help, Im stuck

    Here is my updated code (doesnt crash) until now but there is something wrong with my loop math part because i dont get the answeres im supposed to be getting.


    Code:
    Public Class GoldenRatioForm
    
    
        Private Sub calculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles calculateButton.Click
            'declare variables
            Dim input As Double
            Dim fib1 As Integer
            Dim fib2 As Integer
            Dim fib3 As Integer
            Dim ratio As Double
            Dim ans As Double
            Const GOLDEN_RATIO As Double = 1.618
    
            'assign user input
            input = Val(inputTextBox.Text)
    
            'assign fibonacci numbers
            fib1 = 0
            fib2 = 1
            fib3 = fib1 + fib2
            Dim display As String = "Fibonacci numbers generated:" & ControlChars.NewLine
            display &= fib1 & ", " & fib2
    
    
    
            Do
                fib1 = fib2
                fib3 = fib1 + fib2
                ratio = fib3 / fib2
                ans = ratio - input
                display &= ", " & fib3
            Loop Until ans >= GOLDEN_RATIO
    
            input = Math.Abs(GOLDEN_RATIO - ratio)
    
            fibListBox.Text = display
    
        End Sub
    End Class

  13. #13
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Fibonacci Help, Im stuck

    fib1 = fib2
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  14. #14

    Thread Starter
    Junior Member
    Join Date
    May 2013
    Posts
    31

    Re: Fibonacci Help, Im stuck

    I'm guessing that's a problem *__* my name is newvbusernoob for a reason I guess.

  15. #15

    Thread Starter
    Junior Member
    Join Date
    May 2013
    Posts
    31

    Re: Fibonacci Help, Im stuck

    Isn't that a continuation after I declared the first two Fibonacci numbers? I'm not sure. I'm having a problem with that math/logic in
    my loop

  16. #16
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Fibonacci Help, Im stuck

    Well it is, but it's nothing to do with code. It's pure arithmetic. When fib1 = 0, fib 2 = 1 so ....

    vb.net Code:
    1. fib1 = fib2 ' now fib1 = 1, fib2 = 1
    2.             fib3 = fib1 + fib2 ' fib3 = 2

    I haven't studied your code in depth but surely what you should be doing is

    vb.net Code:
    1. fib1 = fib2
    2. fib2 = fib3
    3. fib3 = fib1 + fib2
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  17. #17
    Hyperactive Member
    Join Date
    Oct 2004
    Posts
    259

    Re: Fibonacci Help, Im stuck

    Quote Originally Posted by Waxy View Post
    .

    check this link

    fib1 = 0
    fib2 = 1
    fib3 = fib1 + fib2

    ....

    fib1 = fib2
    fib2 = fib3
    fib3 = fib1+fib2

    where does it crash?

    your output sounds like you just need a textbox and have that TextboxName.Text = display
    ....
    ----------------------------------------------------

    Missing the days of GWBasic 1.1
    WaxyStudios.com

  18. #18

    Thread Starter
    Junior Member
    Join Date
    May 2013
    Posts
    31

    Re: Fibonacci Help, Im stuck

    I figured it out heres the full code for the working program

    Code:
    Public Class GoldenRatioForm
    
    
        Private Sub calculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles calculateButton.Click
            'declare variables
            Dim input As Double
            Dim fib1 As Integer
            Dim fib2 As Integer
            Dim fib3 As Integer
            Dim ratio As Double
          Dim ans As Double
          Dim counter As Integer
          Const GOLDEN_RATIO As Decimal = 1.618
    
          'assign user input
            input = Val(inputTextBox.Text)
    
    
            'assign fibonacci numbers
            fib1 = 0
            fib2 = 1
            fib3 = fib1 + fib2
            Dim display As String = "Fibonacci numbers generated:" & ControlChars.NewLine
          display &= fib1 & ", " & fib2 & ", " & fib3
          'declare ratio and counter
          ratio = fib3 / fib2
          ans = Math.Abs(GOLDEN_RATIO - ratio)
          counter = 3
          'Implement Loop
          Do While (ans >= input)
             fib1 = fib2
             fib2 = fib3
             fib3 = fib1 + fib2
             ratio = fib3 / fib2
             ans = Math.Abs(GOLDEN_RATIO - ratio)
             display &= ", " & fib3
             counter += 1
          Loop
    
    
          'assign answer to List Box 
          fibListBox.Text = display
          counterLabel.Text = counter
       End Sub
    End Class

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width