Advertisement
viking_unet

fibonachi

Aug 26th, 2020
979
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.83 KB | None | 0 0
  1. # 1 1 2 3 5 8 13 21 44
  2. import sys
  3. import time
  4.  
  5. sys.setrecursionlimit(9999999)
  6.  
  7. def fibonachi(n):
  8.     last1 = 1
  9.     last2 = 1
  10.     cur = 0
  11.     for i in range(1, n+1):
  12.         if i == 1:
  13.             cur = 1
  14.         elif i == 2:
  15.             cur = 1
  16.         else:
  17.             cur = last1 + last2
  18.             last1 = last2
  19.             last2 = cur
  20.     return cur
  21.  
  22. t1 = time.monotonic()
  23. for n in range(0, 35):
  24.     res = fibonachi(n)
  25.     #print(res)
  26. t2 = time.monotonic()
  27. print('indus style: %.6f' % (t2-t1))
  28.    
  29. def fibonachi_rec(n):
  30.     if n <= 0: return 0
  31.     elif n == 1 or n == 2:
  32.         return 1
  33.     else:
  34.         return fibonachi_rec(n-1) + fibonachi_rec(n-2)
  35.  
  36. t1 = time.monotonic()    
  37. for n in range(0, 35):
  38.     res = fibonachi_rec(n)
  39. t2 = time.monotonic()
  40. print('recursion style: %.6f' % (t2-t1))
  41.  
  42.  
  43.  
  44.    
  45.  
  46.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement