#!/usr/bin/env python
'''
    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.

    $ time ./2.py -n 4000000
    n : 4000000
    4613732

    real    0m0.023s
    user    0m0.024s
    sys     0m0.000s

'''
import argparse

parser = argparse.ArgumentParser()

parser.add_argument('-n', action='store', dest='n',
                    default=10,
                    help='n, number')

cmd_args = parser.parse_args()
N = int(cmd_args.n)

print("n : {}".format(N))

def sum_even_fibonacci(n):
    '''
        even fibonacci number is :
            n3 = 4*n2 + n1
            starting
            n1 = 0 and n2 = 2
    '''

    if n < 2:
        return 0

    n1 = 0
    n2 = 2

    sum = n2

    while n2 <= N:

        n3 = 4*n2 + n1

        if n3 > N :
            break;

        n1 = n2
        n2 = n3

        sum += n2

    return sum

print sum_even_fibonacci(N)
