Project Euler Question 2
In this question, we are supposed to find the sum of the even Fibonacci numbers which do not exceed four million. Each term in the Fibonacci sequence is generated by adding the previous two terms. The first two elements of the sequence are 1 and 2.
def fibonacci():
# initializing the first 2 terms
x, y = 1, 2
# num holds the latest number in the generated sequence and check if it's
# under four million.
num=0
result=0
while num<4000000:
# the latest number in the sequence is stored in num, x and y are updated
num=x+y
x=y
y=num
# check if num is even
if num%2==0:
result=num+result
return result
result=fibonacci()
print(result)