首页 » Algorithm, Project Eular, Python » Project Eular #2 (Python)
14

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, …
Find the sum of all the even-valued terms in the sequence which do not exceed four million.

这道题也很水,所以不多说了…

#!/usr/bin/env python

sum = 0
num1 = 0
num2 = 1
while num2 <= 4000000:
    print num2
    if num2 % 2 == 0:
        sum += num2
    temp = num2
    num2 = num1+num2
    num1 = temp
print sum

, ,

发表评论