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

Problem 2

时间:2019-05-31 19:58:03      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:turn   sequence   and   exce   new   starting   ret   tin   else   

# Problem_2.py
"""
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.
"""
def fibonacci(x=1, y=1, end=10000):
    if x >= end:
        return []
    else:
        return [x] + fibonacci(y, x+y, end)


fib = fibonacci(end=4000000)
print(fib)
num = sum([i for i in fib if i%2==0])
print(num)

 

Problem 2

标签:turn   sequence   and   exce   new   starting   ret   tin   else   

原文地址:https://www.cnblogs.com/noonjuan/p/10956931.html

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