Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Uses python3
- import sys
- # too slow memory exceeded
- def get_fibonacci_last_digit_naive(n):
- if n <= 1:
- return n
- previous = 0
- current = 1
- for _ in range(n - 1):
- previous, current = current, previous + current
- return current % 10
- # faster solution only storing the last digit
- def fib_last_digit_large_number(n):
- if n <= 1:
- return n
- l = [0, 1]
- i = 2
- while i < n + 1:
- # stores only the last unit digit of every number which turns out is all you need
- # to compute the next addition
- l.append((l[i - 1] + l[i - 2]) % 10)
- i += 1
- return l[n]
- if __name__ == '__main__':
- # user_input = sys.stdin.read()
- n = int(input())
- print(fib_last_digit_large_number(n))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement