使用Python的Euler项目2

使用Python的Euler项目2,python,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, 34, 55, 89, ... #Find the sum of all the even-valued terms in the

有人能告诉我为什么这是错误的吗

#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, ...
#Find the sum of all the even-valued terms in the sequence
#which do not exceed four million.


sum=2

list = [1,2]
for x in range(2,100):
    a = list[x-2]+list[x-1]
    print(a)
    list.append(a)
    if a % 2 == 0:
        sum += a
        print('sum', sum)
        if sum >= 4000000:
            break
替换

    sum += a
    print('sum', sum)
    if sum >= 4000000:
        break


你应该将“a”与4000000进行比较,而不是像丹尼尔·罗斯曼所说的“总和”。

这个问题要求的是不超过400万的偶数项的总和。您正在检查总和是否不超过4m。

以下是使用生成器和itertools解决问题的完全不同的方法:

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

import itertools
print sum(n for n in itertools.takewhile(
    lambda x: x <= 4000000, fib()) if n % 2 == 0)

因此,您的代码,即使是错误的(请参阅其他答案),也恰好给出了正确的答案。

我正试图解决同样的问题-虽然我理解这样做的逻辑,但我不理解为什么这样做(输出正确的和)

limit=4000000
s=0
l=[1,2]

而l[-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())))

-M1K3

保持简单,所需时间应少于0.1秒

from datetime import datetime
x, y = 1, 1
total = 0
for i in xrange (1, 100):
    x = x + y
    if x % 2 == 0 and x <= 4000000:
        total += x
    y = y + x
    if y % 2 == 0 and x <= 4000000:
        total += y

print total

starttime = datetime.now()
print datetime.now() - starttime
从日期时间导入日期时间
x、 y=1,1
总数=0
对于X范围内的i(1100):
x=x+y

如果x%2==0和x,尝试运行时会出现什么问题?你得到了一个错误的结果吗?你有错误吗?我也误读了这个问题(但方式不同):顺便说一句,前两个斐波那契数都是1。。。但这当然不会影响偶数项的总和。:)
limit = 4000000
s = 0

l = [1,2]
while l[-1]<limit:
    n = l[-1]+l[-2]
    l.append(n)
    print n
limit = 4000000
s = 0

l = [1,2]
while l[-1]<limit:
    n = l[-1]+l[-2]
    if n % 2 == 0 :
        l.append(n)
        print n
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())))
from datetime import datetime
x, y = 1, 1
total = 0
for i in xrange (1, 100):
    x = x + y
    if x % 2 == 0 and x <= 4000000:
        total += x
    y = y + x
    if y % 2 == 0 and x <= 4000000:
        total += y

print total

starttime = datetime.now()
print datetime.now() - starttime