码迷,mamicode.com
首页 > 其他好文 > 详细

002. 斐波那契数列中的偶数

时间:2014-05-22 23:29:44      阅读:435      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   c   code   tar   

Problem 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.

斐波那契数列中的每一项被定义为前两项之和。要求找出该数列中值为不超过 4 百万的偶数的项之和。

我的python代码:

first = 1
last =2 
sum = 2
while 1 :
    last = first + last;
    first = last - first; 
    if last<4000000 :
        if last%2==0:
            
            sum+=last
    else:
        break;
print sum


你知道有更好的可以留言啊,大家互相学习啊!

002. 斐波那契数列中的偶数,布布扣,bubuko.com

002. 斐波那契数列中的偶数

标签:style   class   blog   c   code   tar   

原文地址:http://blog.csdn.net/hackingwu/article/details/26492107

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!