所有斐波纳契数的总和高达一百万

时间:2013-03-01 01:37:40

标签: python

我刚开始做项目Euler问题2,并且想知道是否有人可以帮我修复这个脚本:

def fib():
    x,y = 0,1
    while True:
        yield x
        x,y = y, x+y

def even(seq):
    for number in seq:
        if not number % 2:
            yield number

def under_a_million(seq):
    for number in seq:
        if number > 4000000:
            break
        yield number   

print sum(even(under_a_million(fib())))

这段代码应该找到所有斐波纳契数的总和,直到一百万。现在我遇到了第7行的一些问题。

3 个答案:

答案 0 :(得分:1)

您的代码可以毫无问题地运行 至少是我的版本之后的版本。

如果您的代码确实出错,可能是因为错误的缩进? 我没有问题地运行这段代码。

注意,问题是“斐波那契序列的值不超过<四>百万,找到偶数值的总和”

所以

def fib():
    x,y = 0,1
    while True:
        yield x
        x,y = y, x+y

def even(seq):
    for number in seq:
        if not number % 2:
            yield number

def under_a_million(seq):
    for number in seq:
        if number > 4000000: # <------------
            break
        yield number   

print sum(even(under_a_million(fib())))
BTW,很高兴看到使用的发电机。这几乎就像Haskell中的懒惰评价一样:)

答案 1 :(得分:1)

你好Seamonkey,欢迎来到SO。我很高兴你比上一次更加努力地解决这个问题:)

我将一步一步地与您一起完成代码。对于简单的问题,我发现使用函数更容易,因为当必须处理全局变量时它们会很烦人

#This part of the code gets the entire fibonacci sequence
list_of_numbers = []
even_fibs = []
x,y = 0,1 #sets x to 0, y to 1
while y <= 4000000: #Gets numbers till 4 million
    list_of_numbers.append(y)
    x, y = y, x+y #updates the fib sequence

在这段代码中,(如果你愿意,可以将它改为函数),我们计算完整的斐波那契数列,最多可达400万。我们还创建了两个列表list_of_numberseven_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

在这里,我测试了列表list_of_numbers中的数字是否均匀。如果是,我将它们添加到even_fibs列表中。您可以阅读列表here

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

这只是输出我们的发现。


由于你似乎喜欢功能,我在这里添加了代码。解释与最高层相同。

#This part of the code gets the entire fibonacci sequence
list_of_numbers = []
even_fibs = []

def fib(list_of_numbers):

    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
    return list_of_numbers

def even(list_of_numbers, 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
    return even_fibs

fib(list_of_numbers) #call fib function

even(list_of_numbers, even_fibs) #Call the even function
#Output
print "Normal:  ", list_of_numbers
print "\nEven Numbers: ", even_fibs
print "\nSum of Even Numbers:  ", sum(even_fibs)

答案 2 :(得分:1)

以下是我对Euler 2挑战的简单功能。在Python 3.0中,我希望它能提供一些见解。

def sum_even_fib(n):#n is the upper_limit value for item in fib list
    list_numbers=[]
    a,b=0,1#initializing
    while b<n:#exits if condition fail
        list_numbers.append(b)
        a,b=b,a+b
    even_fib =[i for i in list_numbers if i%2==0]
    result=sum(even_fib)
    return result