# Project Euler Problem # 2 - Even Fibonacci Numbers
# https://projecteuler.net/problem=2

"""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. """


# Function to test if even
def even_numbers(seq): 
	for number in seq: 
		if number % 2 == 0: 
			yield number

# Function to Generate Fibonacci Numbers.  Uses a tuple which I don't
# fully understand yet.  Code taken from another source
def fib():
	x, y = 0, 1
	while True:
		yield x
		x,y = y, x+y  # tuple

# Function to test if the number is under 4,000,000 
def under_four_million(seq):
	for number in seq:
		if number > 4000000:
			break
		yield number

# Print the sum of all even Fibonacci numbers under 4,000,000
print sum(under_four_million(even_numbers(fib()))) 