python - Prime numbers multiplied by 3 shown as prime numbers -
i trying create function in python checks whether given number prime number, wrote code:
def is_prime(x): if x<2: return false elif x==2: return true else: n in range(2,x): if (x%n)==0: return false else: return true
for reason, every number sum of (prime_number*3) shown prime number (for example, these shown prime numbers: 9,21,15,25...) can see problem code?
this happens, because return is_prime
on first iteration of loop. test if x
dividable 2, return true
if , false
otherwise.
remove else
clause loop , return true
after has ended.
def is_prime(x): if x<2: return false elif x==2: return true else: n in range(2,x): if (x%n)==0: return false return true
Comments
Post a Comment