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:

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

// 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

1 Expert Approved Answer
Step: 1 Unlock

a Typically an iterative function like F i b i F i b i is easier to understand ... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Practical Introduction To Data Structures Questions!