import time t1=time.time()def factorial(n): if n==1 or n==2: return 1 else: return factorial(n-1)+factorial(n-2)f=factorial(30)print(f)delt1=time.time()-t1print(delt1)t2=time.time()a=[1,1]for i in range(2,30): d=a[i-1]+a[i-2] a.append(d)print(i)print(a[-1])delt2=time.time()-t2print(delt2)print('the efficient is ',int(delt1/delt2))
8320400.5144872665405273298320400.0003504753112792969the efficient is 1467
可以看出递归的效率非常低,递归除了装13,并不实用…….