Project Euler Problem 2: Even Fibonacci Numbers¶
The source code for this problem can be found here.
Problem Statement¶
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, \dots\)
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the evenvalued terms.
Solution Discussion¶
Simply iterate over the Fibonacci numbers up to four million and sum all the even ones.
Solution Implementation¶
from itertools import takewhile
from lib.numbertheory import is_even
from lib.sequence import Fibonaccis
def solve():
""" Compute the answer to Project Euler's problem #2 """
upper_bound = 4000000
numbers = [fib_i for fib_i in takewhile(lambda x: x <= upper_bound, Fibonaccis()) if is_even(fib_i)]
answer = sum(numbers)
return answer
expected_answer = 4613732

solutions.problem2.
solve
()¶ Compute the answer to Project Euler’s problem #2