# Find the previous Fibonacci number: java

• Comp Sci
• cgrumiea
In summary, we discussed the Fibonacci Sequence and how each number is formed by summing the two previous numbers. We also defined a method named prevFib that takes a non-zero Fibonacci number and returns the Fibonacci number that comes immediately before it in the sequence. A recursive helper method can be used to solve this problem. However, the most concrete way to find the previous Fibonacci number is to start from scratch and add everything up until the desired number is reached, then look back one.
cgrumiea

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

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

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.

## 1. What is the Fibonacci sequence?

The Fibonacci sequence is a mathematical sequence where each number is the sum of the two preceding ones, starting from 0 and 1.

## 2. What is the previous Fibonacci number?

The previous Fibonacci number is the number that comes before a given number in the Fibonacci sequence.

## 3. How do I find the previous Fibonacci number in Java?

In Java, you can find the previous Fibonacci number by using a loop or recursion to generate the sequence and then finding the number before the given number.

## 4. Can you provide an example of finding the previous Fibonacci number in Java?

Sure, here is an example code using a loop to find the previous Fibonacci number in Java:

// Function to find the previous Fibonacci numberint prevFib(int n) { int a = 0, b = 1, c; for (int i = 2; i < n; i++) { c = a + b; a = b; b = c; } return a;}

## 5. What is the time complexity of finding the previous Fibonacci number in Java?

The time complexity of finding the previous Fibonacci number in Java is O(n) where n is the given number. This is because the algorithm needs to iterate through the Fibonacci sequence to find the previous number.

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