3
m = range(1, 2000000, 2)
sum1 = 2
for x in xrange(1, 2000000, 2):
    for y in m:
        if x != y:
            if x%y == 0:
                m.remove(x)
            if all(x%y != 0):
                sum1 += x

这就是我写的。这是一个问题,试图将所有质数相加低于 200 万。我的问题出在 all() 语句中。我想要发生的是检查 x 是否是素数;只有当每个 x%y 都给出一个余数时,这才是正确的。

另外,如果我使用 a 我可以使用语句(break?)来停止循环,如果 y > x/3 像这样:

 m = range(1, 2000000, 2)
sum1 = 2
for x in xrange(1, 2000000, 2):
    for y in m:
        if y > x/3:
            break
        else:
            if x != y:
                if x%y == 0:
                    m.remove(x)
                if all(x%y != 0):
                    sum1 += x
4

3 回答 3

9

You have to pass a sequence or iterable to all -- it just tests whether or not all the items passed to it evaluate as true. Here's the right way to use all:

>>> all([True, True, True])
True
>>> all([True, False, True])
False
>>> all([x > 5 for x in range(10)])
False
>>> all([x > 5 for x in range(6, 10)])
True
>>> all(x > 5 for x in range(6, 10))
True

That last one is the best, because it takes advantage of short-circuiting.

However, your call to all in your code is pointless. The idea behind your code, it seems to me, is to go through all the values in m and remove those that are divisible by any number between 2 and 2000000. Once you've done that, m will contain only prime numbers.

Of course, your code still won't work if you remove all. That's because you're actually testing whether each number in m is divisible by the numbers [1, 3, 5, 7...1999999]. (That's the sequence signified by xrange(1, 2000000, 2). Because you start at 1, and everything is divisible by 1, your code will count nothing as prime. And then, once you remove 1 from that sequence, anything divisible by 2 will be counted as prime by your code! You should think more carefully about which numbers you actually have to test in your inner loop.

Finally, you should think about how many loops this code will complete. Even once you have this working, it will take a very long time to generate a result. You should test it on smaller numbers first; and then, you should think about how to reduce the number of loops. (And -- only after you've thought about it a bit -- read this.)

But once you have this working, all you have to do is call sum on your list of primes.

于 2012-05-27T12:00:59.200 回答
1

您的使用all不正确,如果您查看它的文档,它需要一个可迭代的。

您可能尝试做的是使用生成器表达式,形式如下:

sum(x**2 for x in range(10))

这与列表理解非常相似

[x**2 for x in range(10)]

然而,all以这种方式使用不会突然停止生成器表达式,如果它找到一个除数。使用any和检查x == 0将很快停止,但由于代码当前已格式化,在认为某些东西之前会检查许多除数。

这会更合适:

primes = []
MAX = 2000000

number = 2
while number < MAX:
    for prime in primes:
        if number % prime == 0:
            number += 1
            continue

    primes.append(number)
    number += 1

total = sum(primes)
于 2012-05-27T12:16:50.287 回答
0

all()接受一个可迭代的参数。在您的情况下,您可以像这样使用它:

all(x%y for y in m)

wherex%y for y in m是生成器表达式。如果我有一个可迭代的

[item1, item2, item3, item4...]

all(iterable)相当于:

item1 and item2 and item3 and item4...'
于 2012-05-27T12:07:04.240 回答