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

def sum_even_to_n_max(n_max: int) -> int:
    """
    Find the sum of even fibonacci numbers up to n_max
    """
    sum_even = 2
    m2no = 1
    m1no = 2
    fib_no = 3
    while fib_no < n_max:
        fib_no = m1no + m2no
        m2no = m1no
        m1no = fib_no
        if fib_no%2 == 0:
            sum_even += fib_no
    return sum_even

if __name__ == "__main__":
    print("Ans is %s" %( sum_even_to_n_max(4000000) ) )
