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


def generate_fib(ceiling: int) -> int:
    fib1 = 1
    fib2 = 1

    while fib2 <= ceiling:
        fib3 = fib1 + fib2
        yield fib3

        fib1 = fib2
        fib2 = fib3


def sum_even_fibs(ceiling):
    fib_sum = 0
    for i in generate_fib(ceiling):
        if not i % 2:
            fib_sum += i

    return fib_sum


if __name__ == '__main__':
    print(sum_even_fibs(4e6))
