-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathevenFibonacci.py
More file actions
42 lines (35 loc) · 1.03 KB
/
evenFibonacci.py
File metadata and controls
42 lines (35 loc) · 1.03 KB
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
"""
Project Euler #2: Event Fibonacci numbers
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 N,find the sum of the even-valued terms.
Input Format
First line contains T that denotes the number of test cases.
This is followed by T lines, each containing an integer, N.
Output Format
Print the required answer for each test case.
"""
def fib(n):
sequence = [1, 2]
for i in range(int(n)):
next = sequence[i]+sequence[i+1]
if next > n:
break
else:
sequence.append(next)
return sequence
def evensTotal(fibList):
total = 0
for num in fibList:
total += num
return total
n = raw_input()
cases = []
for entry in range(int(n)):
cases.append(int(raw_input()))
for case in cases:
evens = [x for x in fib(case) if x%2==0]
total = evensTotal(evens)
print total