# Is_prime problems with code?

#1

I seem to have narrowed it down so that every number I try is_prime(x) on works apart from 9, for whatever reason.

If I could figure out why is_prime() of any other number returns the correct Boolean value, but is_prime(9) returns True, that would be wonderful. It seems to only have an issue with 9...?

``````def is_prime(x):
if x < 2:
return False
elif x ==2 or x==3:
return True
else:
for n in range(2, x - 1):
if x % n == 0:
return False
else:
return True``````

#2

Suppose x = 9
Iteration 1: when `n = 2`, `x % n = 9 % 2 = 1`, which renders the if statement `False`, as 1 not equal to 0. So, `else` statement is executed. So, it returns True for `x = 9` for the first iteration. For the second iteration however, it should return False. But it does not get to second iteration because your function ends as soon as it returns True. Hope this clears up what's wrong with your program.

#3

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