Find the previous Fibonacci number: java

  • Context: Comp Sci 
  • Thread starter Thread starter cgrumiea
  • Start date Start date
  • Tags Tags
    Java
Click For Summary

Discussion Overview

The discussion revolves around finding the previous Fibonacci number in the sequence, specifically through a Java implementation. Participants explore both recursive methods and alternative approaches to identify the Fibonacci number preceding a given non-zero Fibonacci number.

Discussion Character

  • Homework-related
  • Exploratory
  • Technical explanation

Main Points Raised

  • One participant requests assistance with defining a recursive helper method in Java to find the previous Fibonacci number.
  • Another participant suggests considering the problem without programming constraints, asking how to find the previous Fibonacci number using any method.
  • A participant proposes a straightforward approach of summing Fibonacci numbers from the beginning until reaching the target number, then looking back one number in the sequence.
  • A later reply agrees with this approach and encourages coding it in Java, indicating it is a reasonable algorithm.

Areas of Agreement / Disagreement

Participants express uncertainty about the best approach to solve the problem, with some favoring a recursive method and others suggesting a more direct summation method. No consensus is reached on a single solution.

Contextual Notes

Participants express varying levels of familiarity with Java and recursion, which may affect their proposed solutions. There is also a lack of clarity on the implementation details of the recursive method.

Who May Find This Useful

Individuals interested in programming, particularly in Java, and those looking to understand Fibonacci sequences and recursion may find this discussion beneficial.

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
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?
 
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.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
5K
Replies
11
Views
3K
Replies
5
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 3 ·
Replies
3
Views
6K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 19 ·
Replies
19
Views
3K