-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patheuler002.py
52 lines (42 loc) · 1.29 KB
/
euler002.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
50
51
52
"""
Each new term in the Fibonacci sequence is generated by adding the previous two terms.
By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
By considering the terms in the Fibonacci sequence whose values do not exceed four million,
find the sum of the even-valued terms.
"""
# an iterator class of even numbers in the fibonacci
class FibEven:
def __init__(self, cutoff=100):
self.last = 0
self.secondLast = 2
self.cutoff = cutoff
def __iter__(self):
return self
def next(self):
temp = 4 * self.secondLast + self.last
if temp > self.cutoff:
raise StopIteration
self.last = self.secondLast
self.secondLast = temp
return temp
def main():
# input number of test cases
t = int(raw_input().strip())
# for each case
for _ in xrange(t):
n = long(raw_input().strip())
total = 2
last = 0
secondLast = 2
while 4 * secondLast + last <= n:
tmp = 4 * secondLast + last
total += tmp
last = secondLast
secondLast = tmp
print total
if __name__ == '__main__':
# print "This program is being run by itself"
main()
else:
print 'I am being imported from another module'