Problem 2

# 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)

 

上一篇:java数据结构和算法(07)斐波那契数列


下一篇:分治与递归-Fibonacci数列兔子问题