Question

The ­following Implementation of the Fibonacci function is a correct, but inefficient,

def fibonacci(n):

if n <= 2:

return 1

else:

return fib(n - 1) + fib(n - 2)

In more details, the code shown runs very slowly for even relatively small values of n; it can take minutes or hours to compute even the 40th or 50th Fibonacci number. The code is inefficient because it makes too many recursive calls. It ends up recomputing each Fibonacci number many times. Which of the below recursive modification/s is/are more efficient compared to the aforementioned function.

idef fibonacci (n): if n <= 2: return 1 else: return fibonacci2(n, 3, 1, 1) def fibonacci2 (n, i, prev, curr): if n == i: ret

a)

A only

b)

B only

c)

A and B

d)

None of them has a significant improvement

0 0
Add a comment Improve this question Transcribed image text
Answer #1

a) A only is more efficient to compute the large Fibonacci number

because, in option B it needs more space i.e for nth fibonacci numbers it needs n space in dictionary.

also in option B there is two recursive function calls and in option A there is one recursive function calls.

Add a comment
Know the answer?
Add Answer to:
The ­following Implementation of the Fibonacci function is a correct, but inefficient, def fibonacci(n): if n...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Programming Exercise 11.6 Х + | Instructions fib.py >_ Terminal + iit B 1 def fib(n):...

    Programming Exercise 11.6 Х + | Instructions fib.py >_ Terminal + iit B 1 def fib(n): 2 "*"Returns the nth Fibonacci number. " 3 if n < 3: lil 4 return 1 Modify the recursive Fibonacci function to employ the memoization technique discussed in this chapter. The function creates a dictionary and then defines a nested recursive helper function named memoizedFib You will need to create a dictionary to cache the sum of the fib function. The base case of...

  • Below you will find a recursive function that computes a Fibonacci sequence (Links to an external...

    Below you will find a recursive function that computes a Fibonacci sequence (Links to an external site.).   # Python program to display the Fibonacci sequence up to n-th term using recursive functions def recur_fibo(n): """Recursive function to print Fibonacci sequence""" if n <= 1: return n else: return(recur_fibo(n-1) + recur_fibo(n-2)) # Change this value for a different result nterms = 10 # uncomment to take input from the user #nterms = int(input("How many terms? ")) # check if the number...

  • For the following recursive implementation of a method to compute the Fibonacci S integer n, circle...

    For the following recursive implementation of a method to compute the Fibonacci S integer n, circle the line number(s) the them: s) that comprise the three parts of a recursive algorithm and label 1. public static long fibonacci(int n) ( 2 if( 1) 3. return 1; 4. else if (n 2) S. return; 6. else 7. (long fibNminus1 fibonacci(n - 1); 8. long fibNminus2- fibonacci(n -2); 9. long fibN fibNminusl + fibNminus2; 10. return fibN; 12.)

  • 1.Take this recursive Fibonacci implementation and convert it into the caching based version discussed in class....

    1.Take this recursive Fibonacci implementation and convert it into the caching based version discussed in class. Implement your caching to store a maximum of 5 values. Create 2 variations: one that stores all values and one that only stores even values. Make sure that you don't leave any gaps in your cache — if you have 5 cache entries you must cache 5 unique and valid values. Compare the caching implementations to the recursive implementation using the time utility. How...

  • Using java programming. Question 1. Write a recursive function fibo(n) that returns the nth Fibonacci number...

    Using java programming. Question 1. Write a recursive function fibo(n) that returns the nth Fibonacci number which is defined as follows: fibo(0) = 0 fibo(1) = 1 fibo(n) = fibo(n-1) + fibo(n-2) for n >= 2 Question 2. Write a recursive function that calculates the sum of quintics: 1power of5 + 2power of5 + 3power of5 + … + n5 Question 3. Write a program to find a route from one given position to another given position for the knight...

  • Please help with this coding problem, and use the ARM sim# to test run your code....

    Please help with this coding problem, and use the ARM sim# to test run your code. Implement functions in assembly language. You are asked to write a program to compute the Fibonacci numbers, using recursive function calls. Fib(n){if (n == 0 || n == 1) return 1; else return Fib(n-2) + Fib(n-1);}

  • Extra Credit - Fibonacci Function (Lec. 5 topic: Recursive function and runtime stack. Use recursion to...

    Extra Credit - Fibonacci Function (Lec. 5 topic: Recursive function and runtime stack. Use recursion to calculate the Fibonacci Function 1.) Use a recursive function called fib() to calculate the Fibonacci Function for the following values for the variable n. int n = 10; int n = 20; int n = 30; int n = 40; int n = 45; int n = 46; 2.) In addition to calculating and displaying the results, use a "timer" to see how long...

  • Fibonacci function Fib(n) is given below. Fib(n)= Fib(n-1) + Fib(n-2) for n > 1 Fib(n)= 1...

    Fibonacci function Fib(n) is given below. Fib(n)= Fib(n-1) + Fib(n-2) for n > 1 Fib(n)= 1 for n=1 Fib(n)= 0 for n=0 Using following initialization unsigned int *Fib = new unsigned int[30]; and function prototypes void push(int n) unsigned int pop( ) insert first 30 Fibonacci numbers into dynamic array Fib and then pop them to print out first 30 numbers in descending order. The output of your program should be 514229, 317811, 196418, ......, 1, 1, 0

  • Fibonacci Sequence The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5,...

    Fibonacci Sequence The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ... The next number is found by adding up the two numbers before it. The 2 is found by adding the two numbers before it (1+1) The 3 is found by adding the two numbers before it (1+2), And the 5 is (2+3), and so on!         Example: the next number in the sequence above is 21+34 = 55 Source:...

  • def ged(m,n) : if m n == 0: return n else: return god (n, møn) Without...

    def ged(m,n) : if m n == 0: return n else: return god (n, møn) Without typing the code into Python, what are the values of the input arguments of the second recursive call if the function gcd is called from the main program as gcd(537,44)? (Hint: do not count the initial call to the function from the main program) Om = 44, n = 9 Om = 44, n = n = 537 Om = 9, n = 8...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT