Advertisement
jules0707

fibonacci_faster

Jun 1st, 2017
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.49 KB | None | 0 0
  1. # Uses python3
  2.  
  3. # slow from the definition algo
  4. def calc_fib(n):
  5.     if n <= 1:
  6.         return n
  7.  
  8.     return calc_fib(n - 1) + calc_fib(n - 2)
  9.  
  10. # faster storing values in List as we go along
  11. l = [0, 1]
  12.  
  13.  
  14. def fib_faster(n):
  15.     if not 1 < n:
  16.         0
  17.     else:
  18.         [l.append(l[_ - 1] + l[_ - 2]) for _ in range(2, n+1)]
  19.  
  20.     return l[n]
  21.  
  22.  
  23. def fib_faster_last_digit(n):
  24.     return fib_faster(n) % 10
  25.  
  26. n = int(input())
  27. # print(fib_faster(n))
  28. print(fib_faster_last_digit(n))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement