# Is_prime(n) stuck from a long time

#1

function fails on is_prime(2). It returns False when it should return True.

There seems to be no error in the code.

``````def is_prime(x):
n = 2
while True:
if x > 2 and n < x:
if x % n == 0:
return True
break
else:
n += 1
continue
elif x < 2:
return False
elif x == 2 and x == 3:
return True
break
else:
return False``````

#2

here:

``elif x == 2 and x == 3:``

how can x be both 2 and 3? Besides, 3 is already caught by the `if` condition.

#4

Thanks that was my mistake. Now I get the same error at x = 4

``````def is_prime(x):
n = 2
while True:
if x > 3 and n < x:
if x % n == 0:
return True
break
else:
n += 1
continue
elif x < 2:
return False
elif x == 2 or x == 3:
return True
break
else:
return False``````

#5

for a number to be a prime number, it can't divislby by any number in range 2 till x (exclusive)

so here:

``````            if x % n == 0:
return True``````

why do you have return True? If the number is divisble by any number in range, the number is not a prime number, so that should be return False

if the remainder equals zero, it means x is divisble by n

#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.