<p>这里是Python新手。我试图了解这个函数如何检查质数:</p>
<pre><code>from itertools import count, islice
from math import sqrt
def is_prime(n):
if n < 2: return False
return all(n%i for i in islice(count(2), int(sqrt(n)-1)))
</code></pre>
<p>据我所知,你可以检查因子直到和<strong>包括</strong>n的平方根,那么为什么这只测试sqrt(n)-1?我也不清楚函数的<code>return all</code>部分。n%i返回一个整数,余数。为什么这个表达式的计算结果是bool?任何关于这方面的建议都会很好。谢谢!在</p>