又对啦。。。开心~~~~

只是代码可能不符合PEP标准什么的。。。

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(number):
    if number < 1:
        pass
    if number == 1:
        return  1
    if number == 2:
        return  2
    return  Fibonacci(number-1) + Fibonacci(number-2)

sum_even = 0
num = 1
while Fibonacci(num) <= 4000000:

    if Fibonacci(num) % 2 == 0:
        sum_even += Fibonacci(num)
        print 'Num count is', num,'Fibonacci is', Fibonacci(num), 'Sum is',sum_even
    else:
        print 'Num count is', num,'Fibonacci is', Fibonacci(num), 'This number is not even_value.'
    num += 1

输出:

C:\webpy\webpy\Scripts\python.exe C:/pycode/euler.py
Num count is 1 Fibonacci is 1 This number is not even_value.
Num count is 2 Fibonacci is 2 Sum is 2
Num count is 3 Fibonacci is 3 This number is not even_value.
Num count is 4 Fibonacci is 5 This number is not even_value.
Num count is 5 Fibonacci is 8 Sum is 10
Num count is 6 Fibonacci is 13 This number is not even_value.
Num count is 7 Fibonacci is 21 This number is not even_value.
Num count is 8 Fibonacci is 34 Sum is 44
Num count is 9 Fibonacci is 55 This number is not even_value.
Num count is 10 Fibonacci is 89 This number is not even_value.
Num count is 11 Fibonacci is 144 Sum is 188
Num count is 12 Fibonacci is 233 This number is not even_value.
Num count is 13 Fibonacci is 377 This number is not even_value.
Num count is 14 Fibonacci is 610 Sum is 798
Num count is 15 Fibonacci is 987 This number is not even_value.
Num count is 16 Fibonacci is 1597 This number is not even_value.
Num count is 17 Fibonacci is 2584 Sum is 3382
Num count is 18 Fibonacci is 4181 This number is not even_value.
Num count is 19 Fibonacci is 6765 This number is not even_value.
Num count is 20 Fibonacci is 10946 Sum is 14328
Num count is 21 Fibonacci is 17711 This number is not even_value.
Num count is 22 Fibonacci is 28657 This number is not even_value.
Num count is 23 Fibonacci is 46368 Sum is 60696
Num count is 24 Fibonacci is 75025 This number is not even_value.
Num count is 25 Fibonacci is 121393 This number is not even_value.
Num count is 26 Fibonacci is 196418 Sum is 257114
Num count is 27 Fibonacci is 317811 This number is not even_value.
Num count is 28 Fibonacci is 514229 This number is not even_value.
Num count is 29 Fibonacci is 832040 Sum is 1089154
Num count is 30 Fibonacci is 1346269 This number is not even_value.
Num count is 31 Fibonacci is 2178309 This number is not even_value.
Num count is 32 Fibonacci is 3524578 Sum is 4613732

Process finished with exit code 0

求四百万以内Fibonacci(number)数列偶数结果的总和的更多相关文章

  1. 第2章 数字之魅——斐波那契(Fibonacci)数列

    斐波那契(Fibonacci)数列 问题描述 递归算法: package chapter2shuzizhimei.fibonacci; /** * Fibonacci数列递归求解 * @author ...

  2. 2.裴波那契(Fibonacci)数列

    裴波那契(Fibonacci)数列 f(n)= ⎧⎩⎨0,1,f(n−1)+f(n−2),n =0n =1n>1 求裴波那契数列的第n项.(题目来自剑指offer) 1.递归解法,效率很低的解法 ...

  3. 求出10000以内所有的完全数-python

    题目: 如何用python去求出下一个(大于28的)完全数? (求出10000以内所有的完全数) 分析: 如果一个数恰好等于它的因子之和,则称该数为"完全数".各个小于它的约数(真 ...

  4. Algorithms - Fibonacci Number

    斐波那契数列(Fibonacci Number)从数学的角度是以递归的方法定义的: \(F_0 = 0\) \(F_1 = 1\) \(F_n = F_{n-1} + F_{n-2}\) (\(n \ ...

  5. Buge's Fibonacci Number Problem

    Buge's Fibonacci Number Problem Description snowingsea is having Buge’s discrete mathematics lesson, ...

  6. [UCSD白板题] The Last Digit of a Large Fibonacci Number

    Problem Introduction The Fibonacci numbers are defined as follows: \(F_0=0\), \(F_1=1\),and \(F_i=F_ ...

  7. [UCSD白板题 ]Small Fibonacci Number

    Problem Introduction The Fibonacci numbers are defined as follows: \(F_0=0\), \(F_1=1\),and \(F_i=F_ ...

  8. (斐波那契总结)Write a method to generate the nth Fibonacci number (CC150 8.1)

    根据CC150的解决方式和Introduction to Java programming总结: 使用了两种方式,递归和迭代 CC150提供的代码比较简洁,不过某些细节需要分析. 现在直接运行代码,输 ...

  9. Fibonacci number

    https://github.com/Premiumlab/Python-for-Algorithms--Data-Structures--and-Interviews/blob/master/Moc ...

随机推荐

  1. 基于deb包快速搭建内外apt源

  2. linux printk函数学习

    printk与printf的区别在于: printk运行在kernel space,而printf运行在user space. 所以kernel打印log使用printk,而应用程序打印log使用pr ...

  3. python学习笔记(集合的使用)

    集合 集合(set):把不同的元素组成一起形成集合,是python基本的数据类型. 集合元素(set elements):组成集合的成员 为什么需要集合? 集合的作用 1 .列表去重复数据 按照现有知 ...

  4. ASP.NET页面同时发起多个ajax请求,出现阻塞情况

    解决办法:假如页面对Session只读不写,可以在页面的开头加上EnableSessionState="ReadOnly".

  5. Runtime.getRuntime().addShutdownHook(shutdownHook);

    今天在阅读Tomcat源码的时候,catalina这个类中使用了下边的代码,不是很了解,所以google了一下,然后测试下方法,Tomcat中的相关代码如下: Runtime.getRuntime() ...

  6. [week4]每周总结与工作计划

    计算机网络 TAT 小白dp 28号还有一场 背单词 背马克思 python目标80% 熟悉coursera c++模版和 仿函数 人文修养 开学数据库,itercast的sql*2 itercast ...

  7. linux 启动network后报错:device eth0 does not seem to be present, delaying initialization

    问题背景: 在vsphere client中部署ovf模板后启动linux 的network后提示:device eth0 does not seem to be present, delaying ...

  8. 《AngularJS》--指令的相互调用

    转载自http://blog.csdn.net/zhoukun1008/article/details/51296692 人们喜欢AngularJS,因为他很有特色,其中他的指令和双向数据绑定很吸引着 ...

  9. 修改MySQL引擎

    1. 显示MySQL支持的引擎:show engines;

  10. linux开关机命令

    1.reboot重启 2.shutdown -r now 立即重启 root用户使用,与reboot命令相同 3.shutdown -r 10 过10分钟后重启root用户使用 4.shutdown ...