Fill in the code to complete the following method for computing a Fibonacci number.
public static long fib(long index) {
if (index == 0) // Base case
return 0;
else if (index == 1) // Base case
return 1;
else // Reduction and recursive calls
return __________________;
}
A. fib(index - 1)
B. fib(index - 2)
C. fib(index - 1) + fib(index - 2)
D. fib(index - 2) + fib(index - 1)

Respuesta :

Answer:

The answer are LETTERS C AND D.

Explanation:

C. fib(index-1)+ fib(index-2)

D. fib(index-2)+ fib(index-1)

Because the recursive algorithm for Fibonacci numbers is a little more involved than the series calculations in the previous porjects. Base cases for 0, 1 or 2 numbers simply return a value, and all the other numbers make two recursive calls to get the previous two Fibonacci numbers to add together to obtain the current number. The method to calculate a Fibonacci number is recursive, but the code to print the output is not, it uses a for-loop to cylce through the Fibonacci numbers and ratios.