您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Python中的项目Euler#2

5b51 2022/1/14 8:20:46 python 字数 2750 阅读 437 来源 www.jb51.cc/python

背景我坚持这个问题: 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

概述

背景

我坚持这个问题:

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.

我试图发现问题是我的Fibonacci数字生成器,获取偶数的代码,甚至我添加数字无效的方式.

我决定将这些数字存储在列表中.在这里,我创造了它们.

list_of_numbers = [] #Holds all the fibs
even_fibs = [] #Holds only even fibs

然后,我创建了我的发电机.这是一个潜在的问题领域.

x,y = 0,1 #sets x to 0,y to 1
while x+y <= 4000000: #Gets numbers till 4 million
    list_of_numbers.append(y)
    x,y = y,x+y #updates the fib sequence

然后,我创建了一些代码来检查数字是否是偶数,然后将其添加到even_fibs列表中.这是代码中的另一个弱点.

coord = 0
for number in range(len(list_of_numbers)):
    test_number = list_of_numbers [coord]

    if (test_number % 2) == 0:
        even_fibs.append(test_number)
    coord+=1

最后,我显示信息.

print "Normal:  ",list_of_numbers #outputs full sequence
print "\nEven Numbers: ",even_fibs #outputs even numbers
print "\nSum of Even Numbers:  ",sum(even_fibs) #outputs the sum of even numbers

我知道这是一个提出问题的可怕方式,但出了什么问题?请不要给我答案 – 只需指出有问题的部分.

总结

以上是编程之家为你收集整理的Python中的项目Euler#2全部内容,希望文章能够帮你解决Python中的项目Euler#2所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶