Here is a simple recursive function to compute the Fibonacci sequence: This algorithm turns out to be

Question:

Here is a simple recursive function to compute the Fibonacci sequence:

// Recursive Fibonacci generator static long fibr (int n) { // fibr (91) is the largest value that fits in a

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

// Iterative Fibonacci generator static long fibi (int n) { // fibr (91) is the largest value that fits in a

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.

Fantastic news! We've Found the answer you've been seeking!

Step by Step Answer:

Question Posted: