1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Find the previous Fibonacci number: java

  1. Jan 19, 2009 #1
    1. The problem statement, all variables and given/known data
    The first several numbers in the "Fibonacci Sequence" are 0, 1, 1, 2, 3, 5, 8, 13, 21, ... Each number is formed by summing the two previous numbers. Define a method named prevFib that takes a non-zero Fibonacci number and returns the Fibonacci number that comes immediately before it in the sequence.

    prevFib(21) ==> 13

    Hint: use a recursive helper method.

    2. Relevant equations



    3. The attempt at a solution
    I'm brand new at java and hardly understand the recursion within it. I know scheme, but this is a different ball game. My main question is what I should do for the recursive helper. Or how that will help when finding the previous Fib number. Any help would be appreciated.
     
  2. jcsd
  3. Jan 19, 2009 #2

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Forget java for a moment! Suppose you were asked to find the Fibonacci number that comes before 1346269, by any means at all. How would you go about doing it?
     
  4. Jan 19, 2009 #3
    Forgive me if I sound like a total idiot here. I'm just genuinely lost.
    I guess one way would be to start from scratch and add everything up until you get there, then look back one. That just seems like the most concrete way to me, since I can't seem to come up with a formula or anything.
     
  5. Jan 19, 2009 #4

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    That sounds like a perfectly reasonable algorithm. Now code it up in Java.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Find the previous Fibonacci number: java
Loading...