def is_prime(x): if x < 2: return False count = 0 for n in range(2, x - 1): if x % n == 0: count = count + 1 if count > 0: return False else: return True print is_prime(9)
As you can see, I used
if count > 0 along with
if x % n == 0:
count = count + 1
to ensure that if there was at least one instance of a number dividing by x with no remainder, it would evaluate to False.
Is there a more efficient way of doing this check?