
# 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.
#
# Expected Answer: 4613732


def findNextFibNum():
    """
     Finds the next number in a Fibonacci sequence

     NOTE: this is a generator function
    :return:
    """
    x, y = 1, 1
    while True:
        yield y
        x, y = y, x + y


def findEvenFibSum(maxVal):
    """
     loops through each Fibonacci number if even adds to evenSum Value
    :param maxVal: generate Fibonacci sequence up to this value
    :return: the sum of all even Fibonacci sequence
    """
    evenSum = 0
    for i in findNextFibNum():
        if i >= maxVal:
            break
        if i % 2 == 0:
            evenSum += i

    return str(evenSum)


if __name__ == "__main__":
    print("Fibonacci number: " + findEvenFibSum(4000000));