项目Euler:Python中的#2

时间:2014-09-24 21:01:25

标签: python fibonacci

提出的问题如下:

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.

我在下面看到的代码上尝试了一些变体。我目前从我编写的代码中获得了数字2,435,424作为答案,但是项目Euler说这个数字是不正确的。我试图改变查看我的代码失败的原因,我很难过。任何意见,将不胜感激。代码如下:

fibonacci = [2]

i = 0

number_1 = 1
number_2 = 2
number_3 = 0

while (number_3 <= 4000000):
    number_3 = number_1 + number_2
    fibonacci.append(number_3)

    if i % 2 != 0:
        number_1 = number_3
        i += 1
    elif i % 2 == 0:
        number_2 = number_3
        i += 1

total = 0

for numbers in fibonacci:
    if numbers % 2 == 0:
        total += numbers

print total

6 个答案:

答案 0 :(得分:2)

考虑many ways您可以在Python中编写Fibonacci序列。

最常见的Pythonic&#39;,恕我直言,是generator

def fib():
    a, b = 0, 1
    while 1:
        yield a
        a, b = b, a + b

您可以使用a % 2的限制和测试来修改它:

def Fib_in_even(lim):
    a, b = 0, 1
    while a < lim:
        if not a % 2:         
            yield a
        a, b = b, a + b

然后使用sum将修改过的斐波纳契系列加到答案中

>>> sum(Fib_in_even(4000000))  
the_answer...

答案 1 :(得分:1)

首先,你的循环会在列表中添加一个太多的值。考虑如果number_3等于400万,会发生什么。然后,您的循环将计算number_3的新值,该值将超过400万,因为number_1number_2中的一个刚刚设置为等于number_3,并添加它到你的清单。同样适用于任何number_3 number_3 <= 4000000number_3 + min(number_1, number_2) > 4000000,我只使用400万作为一个可以轻松演示错误的值。

我对一般算法没有评论 - 正在进行的工作是Project Euler的一部分。但值得考虑的是,如果最终值不是400万,你可能会做什么,但是太大而无法将所有斐波纳契术语同时保留在内存中。

答案 2 :(得分:1)

你正在混合做项目euler要求的总和以及斐波那契数字的实际计算。在混合它的过程中,你会弄乱两半。

让我们一次做一个:

fibonacci = [1, 2]
number_1 = 1
number_2 = 2
number_3 = 3

while (number_3 <= 4000000):
    number_1, number_2, number_3 = number_2, number_3, number_1 + number_2
    fibonacci.append(number_3)

现在,我们有一个斐波那契数字列表。让我们对偶数进行求和。

total = 0
for numbers in fibonacci:
    if numbers % 2 == 0:
        total += numbers

或更简单:

total = sum(x for x in fibonacci if x % 2 == 0)

您绝对想在Peter DeGlopper's answer中应用这些建议。

答案 3 :(得分:0)

您在第一次迭代时替换number_2。这是不正确的。

在这种情况下,无需评估额外费用!整数%2为0或1,因此请使用else。 最重要的是使用if / else在这里没有多大意义,你可以改为旋转。尝试用手工做,你会看到。

项目Euler更多的是学习如何找到具有良好代码和快捷方式的解决方案(原来有400万,并且无法通过遍历两个分支的错误递归获得)。所以我不会在这里包含任何Project Euler问题的确切解决方案,而是指向正确的方向。 我强烈建议学习python生成器(请参阅dawg的答案),因为这是学习和理解它们的最简单的例子。

此外,最好将运行总计保留在主循环中,这样您就不必再次浏览它们了。

关于Project Euler的注意事项:Python对整数没有限制(如果你愿意,你可以有无限的精度),所以有些问题不会有多大意义。此外,RAM和CPU呈指数增长;所以考虑用40亿而不是400万来解决这个问题,你会学到更多。这就是无用的elif可能很昂贵的地方,并且因为你必须跟踪整个结构,所以环绕两次甚至更糟。 可以这样想一想:你能否解决问题而不需要在内存中保留更多必要的变量?这就是生成器,xrange等非常方便的地方(python 2.x)。

答案 4 :(得分:0)

def FibSeries(first,second):
    yield first
    while True:
        first,second = second,first+second
        yield first

fib_lt_4mil = itertools.takewhile(lambda n:n<4000000,FibSeries(1,1))
even_fib_lt_4mil = [n for n in fib_lt_4mil if n%2 == 0]
print sum(even_fib_lt4mil)

至少我认为

答案 5 :(得分:0)

def EvenFibonacciNumbersSum(n):
    a = 1
    b = 2
    temp = 0
    sum =0
    while(a<=n):
        if(a%2 ==0):
            sum = sum + a
        #print(sum)
        temp = a
        a = b
        b = temp+b
    return sum

if __name__ == '__main__':
  print(EvenFibonacciNumbersSum(4000000))