re: Project Euler #2 - Even Fibonacci numbers VIEW POST

FULL DISCUSSION
 

4,613,732! :D

"""Find the sum of even Fibonacci numbers less than 4,000,000"""

def fibonacci(cap):
    """Generates fibonacci numbers less than cap"""
    a, b, c = 0, 1, 1
    while c < cap:
        yield c
        a, b = b, c
        c = a + b

def even_fib_sum(cap):
    """Returns the sum of even fibonacci numbers less than cap"""
    return sum(fib for fib in fibonacci(cap) if fib % 2 == 0)

if __name__ == "__main__":
    print(even_fib_sum(4000000))
    print(list(fibonacci(4000000))

# => 4613732
# => [1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578] 
code of conduct - report abuse