Q.1) Run each of the following codes in your favorite program. All lines to report the execution time. Which algorithm is fastest?

Iterative Approach

12345678910111213141516171819def fib_iter(n):a=1b=1if n==1:print(‘0’)elif n==2:print(‘0′,’1′)else:print(“Iterative Approach: “, end=’ ‘)print(‘0′,a,b,end=’ ‘)for i in range(n-3):total = a + bb=aa= totalprint(total,end=’ ‘)print()return bfib_iter(5)

TEST THE CODE (Links to an external site.)

Output : Iterative Approach : 0 1 1 2 3

Recursive Approach

123456789101112def fib_rec(n):if n == 1:return [0]elif n == 2:return [0,1]else:x = fib_rec(n-1)# the new element the sum of the last two elementsx.append(sum(x[:-3:-1]))return xx=fib_rec(5)print(x)

TEST THE CODE (Links to an external site.)

Output – 0, 1, 1, 2, 3

Dynamic Programming Approach

12345678910111213141516171819202122There is a slight modification to the iterative approach. We use an additional array.def fib_dp(num):arr = [0,1]print(“Dynamic Programming Approach: “,end= ‘ ‘)if num==1:print(‘0’)elif num==2:print(‘[0,’,’1]’)else:while(len(arr)<num):arr.append(0)if(num==0 or num==1):return 1else:arr[0]=0arr[1]=1for i in range(2,num):arr[i]=arr[i-1]+arr[i-2]print(arr)return arr[num-2]fib_dp(5)

TEST THE CODE (Links to an external site.)

Output – 0, 1, 1, 2, 3

If you found this blog helpful, learn artificial intelligence (Links to an external site.) and power ahead in your career. Learn from the best in the industry and also gain access to mentorship sessions and career assistance.

From <https://www.mygreatlearning.com/blog/fibonacci-series-in-python/#fibonacciDynamic (Links to an external site.)>

Do you similar assignment and would want someone to complete it for you? Click on the ORDER NOW option to get instant services at essayloop.com

Do you have a similar assignment and would want someone to complete it for you? Click on the ORDER NOW option to get instant services at essayloop.com. We assure you of a well written and plagiarism free papers delivered within your specified deadline.