projecteuler---->problem:2

来源:互联网 发布:js设计模式6大原则 编辑:程序博客网 时间:2024/05/18 17:59

题目:

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.

翻译:

Fibonacci数列的每后一项都是前面两项的和,若开始的头两项是1和2,那么前10个项就是:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

请求出以上Fibonacci数列中不超过400,0000的所有偶数的和。

解答:

sum,a,b,c=2,1,2,3while(1):c=a+ba = bb = cif c > 4000000:breakif c % 2 == 0: sum += cprint sum


0 0