Checks if the provided integer is a prime number.
- Return
False
if the number is0
,1
, a negative number or a multiple of2
. - Use
all()
andrange()
to check numbers from3
to the square root of the given number. - Return
True
if none divides the given number,False
otherwise.
代码实现
from math import sqrt
def is_prime(n):
if n <= 1 or (n % 2 == 0 and n > 2):
return False
return all(n % i for i in range(3, int(sqrt(n)) + 1, 2))
使用样例
is_prime(11) # True