-
Notifications
You must be signed in to change notification settings - Fork 0
/
fibonacci.py
49 lines (34 loc) · 1.01 KB
/
fibonacci.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
42
43
44
45
46
47
48
49
#!/usr/bin/env python3
def last_8(some_int):
"""Return the last 8 digits of an int
:param int some_int: the number
:rtype: int
"""
return int(str(some_int)[-8:])
def optimized_fibonacci(i):
if i == 0:
return 0
if i == 1:
return 1
nums = [0, 1]
for j in range(2, i + 1):
nums.append(sum(nums))
if j == i:
return nums[-1]
nums.pop(0)
class SummableSequence(object):
def __init__(self, *initials):
self.initials = list(initials)
def __call__(self, i):
if i <= len(self.initials) - 1:
return self.initials[i]
nums = self.initials[:]
for j in range(len(self.initials), i + 1):
nums.append(sum(nums))
if j != i:
nums.pop(0)
return nums[-1]
if __name__ == "__main__":
print("f(100000)[-8:]", last_8(optimized_fibonacci(100000)))
new_seq = SummableSequence(5, 7, 11)
print("new_seq(100000)[-8:]:", last_8(new_seq(100000)))