1. Fibonacci Series
#1defFib (n):ifn = = 1orn = = 2: return1; Else: returnFIB (n-1) + fib (n-2)#2defFib (n):return1 andN <= 2orFIB (n-1) + fib (n-2)#3Fib =LambdaN:1ifN <= 2ElseFIB (n-1) + fib (n-2)#4defFib (N): x, y= 0, 1 while(N): x, y, n= y, x + y, n-1returnx#5Fib =LambdaN, x = 0, y = 1:xif notNElseFib (n-1, y, x +y)#6defFib (n):deffib_iter (n, x, y):ifn = =0:returnxElse: returnFib_iter (n-1, y, x +y)returnFib_iter (n, 0, 1)#7deffib (n):defM1 (A, B): M=[[],[]] m[0].append (a[0][0]*b[0][0]+a[0][1]*b[1][0]) m[0].append (a[0][0]*b[0][1]+a[0][1]*b[1][1]) m[1].append (a[1][0]*b[0][0]+a[1][1]*b[1][0]) m[1].append (a[1][0]*b[1][0]+a[1][1]*b[1][1]) returnmdefm2 (A, b): M=[] M.append (a[0][0)*b[0][0]+a[0][1]*b[1][0]) m.append (a[1][0]*b[0][0]+a[1][1]*b[1][0])returnmreturnM2 (reduce (m1,[[[0,1],[1,1]) forIinchRange (n)]), [[[0],[1]]) [0]
Pythonic Code in Several Lines