- Feb 3, 2005
- 7,326
- 2
- 76
I'm having a bit of trouble on a homework problem:
-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.
I've made a code that makes the sequence and returns the number based on the "index" value you choose. I'm still trying to figure out how to tie that in to my prevFib method, if I can at all.
My only idea is that I have my "helper" code (the one that creates the sequence and returns a specific index value) to remember the current and previous values in the sequence. The prevFib code would somehow have to match up the input value with the current value in the helper and find the previous number based on that.
That may be totally off, and I'm aware of that. That's just the only method I can think of.
I'm also very new to Java. We've only had one lecture to go over it, and nothing has been nearly this complicated (not that this is super complicated). So, my skills in coding are also a big barrier.
Thanks for any help
-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.
I've made a code that makes the sequence and returns the number based on the "index" value you choose. I'm still trying to figure out how to tie that in to my prevFib method, if I can at all.
My only idea is that I have my "helper" code (the one that creates the sequence and returns a specific index value) to remember the current and previous values in the sequence. The prevFib code would somehow have to match up the input value with the current value in the helper and find the previous number based on that.
That may be totally off, and I'm aware of that. That's just the only method I can think of.
I'm also very new to Java. We've only had one lecture to go over it, and nothing has been nearly this complicated (not that this is super complicated). So, my skills in coding are also a big barrier.
Thanks for any help
