擅长:python、mysql、java
<p>你的代码实际上已经接近功能性了。你的条件有一个逻辑错误。在</p>
<p>您可以对<a href="http://en.wikipedia.org/wiki/Primality_test" rel="nofollow">primality test</a>进行一些优化,比如只检查给定数字的平方根。在</p>
<pre><code>def is_prime(x):
if x >= 2:
for i in range(2,x):
if x % i == 0: # <----- You need to be checking if it IS evenly
print "not prime" # divisible and break if so since it means
break # the number cannot be prime
else:
print "ok"
else:
print "prime"
else:
print "not prime"
</code></pre>