简体   繁体   English

用列表理解测试素数

[英]testing for prime number with list comprehension

I am trying to test if a number is a prime number using list comprehension.我正在尝试使用列表理解来测试一个数字是否是素数。 This is what?I have done, but not getting the desired results.这是什么?我做了,但没有得到预期的结果。

number = 20
prime_number_test = [1 if number % i != 0 else 0 for i in range(2, 5)]
output = [0,1,0]

number 20 obviously isn't a prime number. 20 显然不是质数。 Now if I have a known prime number to test like let say number 19现在,如果我有一个已知的素数要测试,比如让数字 19

number = 19
prime_number_test = [1 if number % i != 0 else 0 for i in range(2, 5)]
output = [1,1,1]

Now my question is, why is the first code not all zeros, and what is the best way to achieve this using a list comprehension that my results tells me this is a prime number现在我的问题是,为什么第一个代码不是全为零,以及使用列表理解实现这一点的最佳方法是什么,我的结果告诉我这是一个素数

the above code show [0,1,0] because 20 is not divisible by 3 but is divisible by 2 and 4. 19 is not divisible by 2,3 or 4 so it has the output [1,1,1] You can say that if there are 0 in output then the number is not prime.上面的代码显示[0,1,0]因为 20 不能被 3 整除,但可以被 2 和 4 整除。19 不能被 2,3 或 4 整除,因此它具有 output [1,1,1]你可以说如果 output 中有 0 那么这个数字不是素数。

you can use the following program to find primes.您可以使用以下程序来查找素数。

num = int(input("Enter a number: "))  
  
if num > 1:  
   for i in range(2,num):  
       if (num % i) == 0:  
           print(num,"is not a prime number")  
           print(i,"times",num//i,"is",num)  
           break  
   else:  
       print(num,"is a prime number")  
         
else:  
   print(num,"is not a prime number")  

Your comprehension would give you a list of values, and you can use the any function.你的理解会给你一个值列表,你可以使用any function。 For example, here a list of True or False values is obtained:例如,这里获得了TrueFalse值的列表:

number = 20
upper_limit = int(number ** 0.5)  # square root
prime_number_test = not any([number % i == 0 for i in range(2, 1 + upper_limit)])
print(prime_number_test)

The list returned by your comprehension is:您的理解返回的列表是:

[True, False, True]

corresponding to division by 2, 3, and 4.对应于除以 2、3 和 4。

Then the any returns True , which the not then negates: 20 is not a prime number because it is divisible by 2 and 4.然后any返回True ,然后not否定:20 不是质数,因为它可以被 2 和 4 整除。

Here you could also invert the test and do:在这里,您还可以反转测试并执行以下操作:

prime_number_test = all([number % i != 0 for i in range(2, 1 + upper_limit)])

Note the != and the all .注意!=all

However, in either case, it is more efficient (in time and memory) if you replace the square brackets with parentheses:但是,无论哪种情况,如果用括号替换方括号,效率会更高(在时间和内存上):

prime_number_test = not any((number % i == 0 for i in range(2, 1 + upper_limit)))

This similar looking expression uses a generator , and instead of building up a list it will generate values on demand and the any will cause it to iterate as required and stop when the first match is found.这个看起来相似的表达式使用了一个生成器,而不是构建一个列表,它将按需生成值,并且any将导致它根据需要进行迭代并在找到第一个匹配项时停止。

(When using the generator as the sole argument of a function, as here, it is also permissible to omit the outer parentheses.) (当使用生成器作为 function 的唯一参数时,也可以省略外括号。)

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

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