Question: Exercise 2 . Fibonacci sequence ( 5 5 pts ) . The famous Fibonacci Sequence is the series of numbers like this: 0 , 1

Exercise 2. Fibonacci sequence (55pts).
The famous Fibonacci Sequence is the series of numbers like this: 0,1,1,2,3,5,8,13,21,34,... The next number is found by adding up the two numbers prior to it.-2 is found by adding the two numbers before it (1+1)-3 is found by adding the two numbers before it (1+2),-5 is (2+3),-......
An implementation could be like this:
def fibonacci(num):
if num <2:
return num
return fibonacci(num -1)+ fibonacci(num -2)
But the runtime performance is terrible. This is because the code keeps recalculating Fibonacci numbers that are already known. 1.
Implement a @cache decorator that will save the calculations in a function attribute dictionary. Even though the function fibonacci(num) only has one input argument, please make the decorator work for functions with any number of arguments (the decorators own arguments can be ignored).

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock 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 Programming Questions!