作为一个新手python爱好者,我觉得这很烦人:
def isPrime(x):
if x < 0: raise Exception("The number is negative.")
if x == 0 or x == 1: return False
if x == 2: return True
else:
if x % 2 == 0: return False
for i in xrange (3, int(math.sqrt(x)), 2): #-------> This doesn't do anything.
if x % i == 0: return False # Even if I put 3 instead of i, it still prints numbers that are divisible by 3.
return True
for i in xrange (100):
if isPrime(i):
print i
我得到像 9、15、21 这样的数字——可被 3 整除,因此不是素数。我错过了什么?