-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathproblem2-thegezy.py
More file actions
21 lines (17 loc) · 956 Bytes
/
problem2-thegezy.py
File metadata and controls
21 lines (17 loc) · 956 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
'''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, 144, ...
By considering the terms in the Fibonacci sequence whose values do not exceed four million,
find the sum of the even-valued terms. '''
def Fibonacci(x): #function to generate the Fibonnaci sequence
a, b = 1, 2 #initial terms: 1 and 2
while (a < x):
yield a
a, b = b, a+b #swap values, add to get the next term
print ("The even terms of the Fibonacci sequence less than 4,000,000 are:")
s = 0 #initial sum declared
for y in Fibonacci(4000000): #function call
if (y % 2 == 0): #get the even-valued terms
s += y #increment the sum
print (y) #print the terms
print ("And their sum is ", s) #print the sum