Question: Here is a simple recursive function to compute the Fibonacci sequence: This algorithm turns out to be very slow, calling Fibr a total of Fib(n)
Here is a simple recursive function to compute the Fibonacci sequence:

This algorithm turns out to be very slow, calling Fibr a total of Fib(n) times. Contrast this with the following iterative algorithm:

Function Fibi executes the for loop n - 2 times.
(a) Which version is easier to understand? Why?
(b) Explain why Fibr is so much slower than Fibi.
// Recursive Fibonacci generator static long fibr (int n) { // fibr (91) is the largest value that fits in a long assert (n> 0) && (n
Step by Step Solution
3.25 Rating (154 Votes )
There are 3 Steps involved in it
a Typically an iterative function like F i b i F i b i is easier to understand ... View full answer
Get step-by-step solutions from verified subject matter experts
