Advertisement
rjcostales

fibonacci.py

Mar 6th, 2022
1,179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.56 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. import time
  3.  
  4.  
  5. def fib0(n):
  6.     if n > 1:
  7.         return fib0(n - 1) + fib0(n - 2)
  8.     if n == 1:
  9.         return 1
  10.     if n == 0:
  11.         return 0
  12.  
  13.  
  14. def fib1(n):
  15.     if n > 2:
  16.         return 2 * fib1(n - 2) + fib1(n - 3)
  17.     if n == 1 or n == 2:
  18.         return 1
  19.     if n == 0:
  20.         return 0
  21.  
  22.  
  23. if __name__ == "__main__":
  24.  
  25.     a = 40
  26.  
  27.     start = time.time()
  28.     f = fib0(a)
  29.     stop = time.time()
  30.     print(f, stop - start)
  31.  
  32.     start = time.time()
  33.     f = fib1(a)
  34.     stop = time.time()
  35.     print(f, stop - start)
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement