Project Euler Problem2

Even Fibonacci numbers

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.

 

Answer:

 1 def isEven(a):
 2     if a%2 == 0:
 3         return True
 4     return False
 5 
 6 Max = 4000000
 7 
 8 a = 0
 9 b = 1
10 c = 0
11 count = 0
12 while True:
13     c = a + b
14     if(c > Max):
15         break
16     if isEven(c):
17         count += c
18     a = b
19     b = c
20 
21 print("the result is ", count)

 

posted @ 2013-12-10 16:57  tianxiaozz  阅读(114)  评论(0)    收藏  举报