Java problems

hans030390

Diamond Member
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 :)
 

Onund

Senior member
Jul 19, 2007
287
0
0
Don't get hung up on 'new to java', this is not a Java problem. This is a generic programming concepts problem and can be solved with just about any programming language. Is this the first bit of coding you've done at all?

If you've already created a Fibonacci sequence calculator that stops and returns at a passed value then you should already be done. By definition you must already have the previous Fibonnaci number when you have the current number. It's the way the sequence works. Instead of returning the current number, can you not just return f-1?

Your logic is good for your idea though based on the constraints of your homework problem, which sounds like "get it to work".
 

hans030390

Diamond Member
Feb 3, 2005
7,326
2
76
Originally posted by: Onund
Don't get hung up on 'new to java', this is not a Java problem. This is a generic programming concepts problem and can be solved with just about any programming language. Is this the first bit of coding you've done at all?

If you've already created a Fibonacci sequence calculator that stops and returns at a passed value then you should already be done. By definition you must already have the previous Fibonnaci number when you have the current number. It's the way the sequence works. Instead of returning the current number, can you not just return f-1?

Your logic is good for your idea though based on the constraints of your homework problem, which sounds like "get it to work".

This is the first bit of coding I've ever done with Java. I've done work with Scheme previously, but that's it.

My main problem is that prevFib is given a specific number in the sequence, say 21. My Fibonacci "helper" is given a value of n (index)...so, if I give it a value of n = 9, it will return 21. I'm not sure how to tie that into the prevFib code...it's like a number IN the sequence vs. the number at a specific index value.

I'm sure there's some way to tie them together, but I can't figure it out.

Someone else from my class said the following, which makes me believe I'm on the wrong track:

"I was thinking along the same lines as well until I noticed that there's nothing in the assignment description limiting the number of variables you can pass to a helper procedure.

Have you tried writing a helper function that just adds up the values of the Fibonacci sequence while keeping track of what it's looking for?"
 

Onund

Senior member
Jul 19, 2007
287
0
0
sorry, didn't catch that part about index. So basically your current method is calculating Fibonacci numbers and stopping after you calculated enough numbers? You probably have some check in there that says something like:

if(index == count) {
return fibValue;
}

You should be able to just convert this to something like:

if(targetValue == fibValue){
return prevFibValue;
}

the two methods should be extremely similar, you're just acting on a different condition.

From what you posted it looks like the hint expects you to make a brand new helper method for this problem. Without seeing how your code work I can only guess how you've done your index finder method and how you build the Fibonacci sequence, but I would expect you already have the previous Fibonacci number because you need it to calculate the current number.
 

hans030390

Diamond Member
Feb 3, 2005
7,326
2
76
Thanks for your help, but I ended up figuring it out with help from a guy in my class.

Turns out I was just over thinking it. I did have to make a different helper, though.
 

presidentender

Golden Member
Jan 23, 2008
1,166
0
76
This, along with your other thread, leads me to think that you have trouble with recursion. You might want to work on that in general, rather than specific homework assignments.