Question

Linear time fibonacci - Using memoized recursion. What is Fib(60) and how much time does it...

Linear time fibonacci - Using memoized recursion. What is Fib(60) and how much time does it take for its execution.

need to be done in python. tell what your code is doing and show it working please

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

The above problem can be solved in the following way-

Explanation of code-

Here we are using memoized recursion.

We use this memoization technique along with recursion so that we can avoid re-calculating the values of the same state again and again. That is why when we compute a fibonacci term, we store it in the memory itself(in the F[] array) and check if its already there in memory. If its there, no need to calculate it again.

In the code also, we see that we compute lets say F[5] by computing F[4] and F[3] and adding them up, but since we have already computed F[4] and F[3] previously, on checking if its -1 or not, we see that F[3] and F[4] has some other value than -1, it means it has already been computed, so we do not need to compute again and simply use that value stored in memory itself. In this way, we are reducing the time complexity by a significant margin as it re-calculations are avoided at every step.

PYTHON CODE-

#import time module
import time
#list initialized with -1's to store the fibonacci terms
F = [-1]*101 #global array declaration so that only 1 copy is maintained for fibonacci sequence
#define the function fib()
#takes in a number as parameter
def fib(n):
#if 'n' is 0 or 1, return 'n'
if n<=1:
return n;
#and if F[n] is not -1, it means it is already computed, so return F[n]
elif F[n] != -1:
return F[n]
#and for all other cases
else:
#make a recursive call to fib(n-1) and fib(n-2) and store it in F[n]
F[n] = fib(n-1) + fib(n-2)
#finally return the list
return F[n]
  
'''this code times the function call to fib(60)
we start the timer and store it in start time and then call the function fib(60)
and then end the timer. And then print the time by subtracting end and start'''
start = time.time()
fib(60) #function call
end = time.time()   
print("Time taken for execution in secs = %.16f" % (end-start)) #print the time taken

#print the 60th term of fibonacci series, we can use for loop to print
#the entire fibonacci sequence till 60th term, but here only for demonstration, I've printed 60th term
print('The 60th term of fibonacci sequence is : ',F[60])

IMAGE OF CODE-

10 1 #import time module import time 3 #List initialized with -1s to store the fibonacci terms 4 F = [-1] *101 #global array

OUTPUT-

Time taken for execution in secs = 0.0000476837158203, The 60th term of fibonacci sequence is : 1548008755920 ... Program fin

If this answer helps, please give an up vote and feel free to comment for any query.

Add a comment
Know the answer?
Add Answer to:
Linear time fibonacci - Using memoized recursion. What is Fib(60) and how much time does it...
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
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