for n in range(3,int(a ** 0.5) + 1): if a % n == 0: return False if a % n != 0: return True # This if condition is not needed
You do not need this condition 2 times. otherwise it will return immediately when the number is not divided by the current value of n . But you donβt want it. You need to check the following value if the current n cannot divide your number.
So, just remove this if and add return True after the loop ends.
So your prime() method should look like this: -
def prime(a): if a < 2: return False if a % 2 == 0: return False if a == 3 or a == 5 or a == 7: return True for n in range(3,int(a ** 0.5) + 1): if a % n == 0: return False return True
source share