SHARE
TWEET

Untitled

a guest Oct 14th, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def recursive_fib(n):
  2.     if n <= 1:
  3.         return n
  4.     return recursive_fib(n - 2) + recursive_fib(n - 1)
  5.  
  6.  
  7. def memoized_fib(n):
  8.     f = [0] * (n + 1)
  9.     f[0] = 0
  10.     f[1] = 1
  11.     for i in range(2, n + 1):
  12.         f[i] = f[i - 2] + f[i - 1]
  13.     return f[n]
  14.  
  15.  
  16. @lru_cache(maxsize=None)
  17. def memoized_recursive_fib(n):
  18.     if n <= 1:
  19.         return n
  20.     return memoized_recursive_fib(n - 2) + memoized_recursive_fib(n - 1)
  21.  
  22.  
  23. def last_digit_fib(n):
  24.     return memoized_fib(n) % 10
  25.  
  26.  
  27. def improved_last_digit_fib(n):
  28.     f = [0] * (n + 1)
  29.     f[0] = 0
  30.     f[1] = 1
  31.     for i in range(2, n + 1):
  32.         f[i] = (f[i - 2] + f[i - 1]) % 10
  33.     return f[n]
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top