2: Even Fibonacci numbers#
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.
Your Solution#
Lunch this notebook and try to create your own solution!
Tip: look for the launch button (🚀) in the top right corner!
def problem2(N): # N = Maximum number in the Fibonacci sequence
# Try your solution here
return solution
# Testing:
unittest.main(argv=[''], verbosity=2,exit=False)
# Printing Project Euler Solution
# print(problem2(4_000_000))
My solution#
Spoiler Alert!!
See my solution bellow
def problem2(N):
i = 1
j = 2
solution = 2
while j < N:
k = i + j
i = j
j = k
if(k % 2 == 0):
solution += k
return solution
# Testing
unittest.main(argv=[''], verbosity=2,exit=False)
The sum of the even-valued terms whose values do not exceed four million is:
# Printing Project Euler Solution
print(problem2(4_000_000))
4613732