I am trying to find the last digit of sum of Fibonacci Series. I calculate the sum as F(n+2) - 1
. The below code is working fine but it is slow for large numbers (e.g 99999
).
How can I optimize this?
n = int(input())
def last_digit(n):
a, b = 0, 1
for i in range(n+2):
a, b = b, a + b
return (a-1) % 10
print(last_digit(n))