美文网首页
Even Fibonacci numbers

Even Fibonacci numbers

作者: NoFacePeace | 来源:发表于2017-11-07 14:08 被阅读0次

    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
    
    

    相关文章

      网友评论

          本文标题:Even Fibonacci numbers

          本文链接:https://www.haomeiwen.com/subject/xsodmxtx.html