Even Fibonacci numbers

2017-11-07  本文已影响0人  NoFacePeace

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

代码:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

f1 = 1
f2 = 2
sum = 0
while f1 < 4000000:
    if f1 % 2 == 0:
        sum += f1
    # print f1
    f3 = f1
    f1 =f2
    f2 = f3 + f2
print sum

上一篇下一篇

猜你喜欢

热点阅读