简体   繁体   English

无法弄清楚如何使用all()在我的“代码”中工作

[英]Can't figure out how to use all() to work in my “code”

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

That's what I've written. 那就是我写的 It's about a problem, trying to add all the primes bellow two million. 这是一个问题,试图将所有素数加到200万以下。 My problem is in the all() statement. 我的问题是在all()语句中。 What I want to happen is to check if x is a prime; 我想做的是检查x是否为素数; that is true only if every x%y gives a remainder. 仅当每个x%y都给出余数时才成立。

Also if I use a can I use a statement (break?) to stop the loop if y > x/3 like so: 另外,如果我使用a,那么如果y> x / 3,可以使用语句(break?)来停止循环,如下所示:

 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

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. 您必须向all传递一个序列或可迭代的序列-它只是测试传递给它的所有项目是否都评估为true。 Here's the right way to use all : 这是使用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. 但是,您对代码中all的调用毫无意义。 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. 在我看来,代码背后的想法是遍历m所有值,并删除那些可被2到2000000之间的任何数字整除的值。一旦完成, m将仅包含质数。

Of course, your code still won't work if you remove all . 当然,如果删除all ,您的代码仍然无法正常工作。 That's because you're actually testing whether each number in m is divisible by the numbers [1, 3, 5, 7...1999999] . 这是因为您实际上正在测试m每个数字是否可被数字[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. (这是xrange(1, 2000000, 2)表示的序列。因为您从1开始,并且所有内容都可以被1整除,所以您的代码将不算作素数。然后,从该序列中删除1 ,所有内容都可以被整除您的代码会将2视为素数!您应该更仔细地考虑内部循环中实际上必须测试的数字。

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. 但是一旦完成这项工作,您要做的就是在素数列表中调用sum

Your use of all is incorrect, if you look at the documentation for it, it takes an iterable. 您对all内容的使用all错误的,如果您查看它的文档 ,那将是一个可迭代的过程。

What you may be trying to do is use a generator expression, something of the form: 您可能想做的是使用生成器表达式,其形式如下:

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

which is very similar to the list comprehension 这与列表理解非常相似

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

However, use of all in this manner wouldn't suddenly stop the generator expression, if it found a divisor. 但是,以这种方式使用all函数并不会突然停止生成器表达式(如果找到了除数)。 Use of any and checking for x == 0 would stop sooner, but as the code is currently formatted, would check for many divisors before deeming something prime. 使用any并检查x == 0会更快停止,但是由于代码当前已格式化,因此会在认为素数之前检查许多除数。

This would be more appropriate: 这会更合适:

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)

all() takes an iterable for an argument. all()需要一个可迭代的参数。 In your situation, you would use it like this: 在您的情况下,可以这样使用它:

all(x%y for y in m)

where x%y for y in m is a generator expression. 其中x%y for y in m是生成器表达式。 If I have an iterable 如果我有迭代

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

all(iterable) is equivalent to: all(iterable)等效于:

item1 and item2 and item3 and item4...'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM