Find the previous Fibonacci number: java

  • Context: Comp Sci 
  • Thread starter Thread starter cgrumiea
  • Start date Start date
  • Tags Tags
    Java
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
3 replies · 4K views
cgrumiea
Messages
4
Reaction score
0

Homework Statement


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.

Homework Equations





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.
 
Physics news on Phys.org
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.
 
cgrumiea said:
I guess one way would be to start from scratch and add everything up until you get there, then look back one.
That sounds like a perfectly reasonable algorithm. Now code it up in Java.