[英]Function to check if number x is prime in python
What I've done so far is: 到目前为止,我所做的是:
def check_prime(x):
if x >= 2:
for n in range(2, x - 1):
if x % n == 0:
return False
else:
return True
else:
return False
However when I check if any number >= 2 is a prime it returns None
instead of True
or False
. 但是,当我检查任何数字> = 2是否为质数时,它将返回None
而不是True
或False
。 check_prime(0)
returns False
and check_prime(1)
returns False
. check_prime(0)
返回False
而check_prime(1)
返回False
。 Why does any number >= 2 return None
and how can I fix this. 为什么任何> = 2的数字都返回None
,我该如何解决。
Your function is wrong because you should not return True in the loop (in your code the loop will always run once). 您的函数是错误的,因为您不应在循环中返回True(在您的代码中,循环将始终运行一次)。
def check_prime(x):
if x >= 2:
for n in range(2, x ):
if (x % n) == 0:
return False
#after the complete for n loop
return True
else:
return False
Checkout a working fiddle: http://pythonfiddle.com/check-prime 检出有效的提琴: http : //pythonfiddle.com/check-prime
Besides all that you can obtimize your code (if you like) by taking the square root of x
(round up) as the end point for your loop (do not forget +1 since range
is not inclusive). 除此以外,您还可以通过将x
平方根(向上舍入)作为循环的终点来使您的代码(如果愿意的话)更加有趣(不要忘记+1,因为range
不包括在内)。 Since you will get the mirrored options once your past the square root. 因为一旦经过平方根,您将获得镜像选项。 (6×4 = 4*6 = 20. Square root of 20 is 5). (6×4 = 4 * 6 =20。20的平方根是5)。
Problem 1: The problem is range(n, x-1)
. 问题1:问题是range(n, x-1)
。
If your input is 2 or 3, range(2, x-1)
will be an empty list since the second parameter of range
is exclusive. 如果输入的是2或3,则range(2, x-1)
将是一个空列表,因为range
的第二个参数是独占的。
Since you're only returning inside the for loop and it never gets there, it returns None (ie it returns nothing). 由于您只在for循环内返回并且永远不会到达那里,因此它返回None(即不返回任何内容)。
Problem 2: Aside from never entering the for loop if x = 2
or x = 3
, your code has some issues. 问题2:除了从不进入for循环(如果x = 2
或x = 3
,您的代码还存在一些问题。
As you've written it, it will return in the first iteration. 如您所写,它将在第一次迭代中返回。 Certainly, if x % n == 0
you know x
is not prime and can return False
. 当然,如果x % n == 0
您知道x
不是质数,可以返回False
。 But even if n
is not a factor of x
, you still have to check the other potential factors. 但是,即使n
不是x
的因数,您仍然必须检查其他潜在因素。
You should return True
outside of the for loop, not inside it. 您应该在for循环之外而不是在其中返回True
。
Solution: 解:
if x == 2: return True
if x%2 == 0 or x < 2: return False
for n in range(3, x/2, 2):
if x % n == 0:
return False
return True
Here is your fixed code. 这是您的固定代码。 You had multiple issues in it, namely: 您有多个问题,即:
2
for example, you went into the if
cycle, however your loop did not execute and therefore you got None
as a result. 例如,当number为2
,您进入了if
循环,但是您的循环未执行,因此结果为None
。 range(start, end)
does not include the end
, therefore you have do not have to write end - 1
. range(start, end)
不包括end
,因此您不必编写end - 1
。 n
(well, you don't have to when you optimize your code :)
), however if I had a number like 99 = 3 * 33, it would not be divisible by 2 and would classify it as a prime despite being a composite number. 您需要检查每个数字,最多n
(嗯,优化代码时不必这样:)
),但是如果我有一个像99 = 3 * 33这样的数字,它就不能被2整除,并且会尽管是合成数字,但仍将其分类为素数。 You can do a number of basic optimalizations such as check numbers up to sqrt(n)
, check if number is divisble by 2 to discard half of the choices, etc. 您可以进行一些基本的优化,例如检查最大为sqrt(n)
,检查数字是否被2除以舍弃一半的选择,等等。
' “
def check_prime(x):
for n in range(2, x):
if x % n == 0:
return False
return True
Here's a kludge-tastic way of doing it... 这是一种疯狂的方式...
def prime(x):
factors = []
if x <= 0:
return False
if x == 1:
return False
if x == 2:
return True
for n in range(2, x):
if x % n == 0:
factors.append(n)
if len(factors) != 0:
return False
else:
return True
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.