Q.1) Run each of the following codes in your favorite program. All lines to repo

Q.1) Run each of the following codes in your favorite program. All lines to report the execution time. Which algorithm is fastest?Iterative Approach12345678910111213141516171819def fib_iter(n): a=1 b=1 if 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 + b b=a a= total print(total,end=’ ‘) print() return bfib_iter(5)TEST THE CODE (Links to an external site.)Output : Iterative Approach : 0 1 1 2 3Recursive Approach123456789101112def 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 elements x.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, 3Dynamic Programming Approach12345678910111213141516171819202122There 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)
Requirements: 1100

Leave a comment

Your email address will not be published. Required fields are marked *