-
Notifications
You must be signed in to change notification settings - Fork 1
/
fibonacci_partial_sum.py
41 lines (32 loc) · 1.01 KB
/
fibonacci_partial_sum.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import sys
def fibonacci_partial_sum_naive(from_, to):
sum = 0
current = 0
next = 1
for i in range(to + 1):
if i >= from_:
sum += current
current, next = next, current + next
return sum % 10
def pisano_period(m):
previous=0
current=1
for i in range(m*m):
previous, current = current, (previous + current)%m
if previous==0 and current==1:
return (i+1)
def fibonacci_sum_last_digit_fast(n):
if n <= 1:
return n
previous = 0
current = 1
for _ in range(n - 1):
previous, current = current, (1+previous + current)%10
return current
if __name__ == '__main__':
period=pisano_period(10)
sum_over_period=fibonacci_sum_last_digit_fast(period)
#input = sys.stdin.read();
m, n = map(int, input().split())
print(((fibonacci_sum_last_digit_fast(n%period)+(sum_over_period*(n//period))%10)-(fibonacci_sum_last_digit_fast((m-1)%period)+(sum_over_period*((m-1)//period))%10))%10)
#num_periods