擅长:python、mysql、java
<ol>
<li><p>该函数用于正确检查sqrt(n)。因为islice(count(2),sqrt(n)-1意味着count sqrt(n)-1从2开始的数字。在检查素数时,从2到它的平方根就足够了,因为即使有一个因子大于平方根,它也会有一个小于平方根的相应因子。在这里使用int(sqrt(n)),意味着我们要检查一个额外的数字-没有危害,但没有必要。使用int(sqrt(n)-1)意味着我们只进行必要的比较。</p></li>
<li><p>如果迭代的所有元素的计算结果都为true,则all()将返回true。在python中,0的计算结果为false。这意味着,如果对于2和sqrt(n)之间的任何数字,整数除法的余数为0,则all()将返回false。这是正确的,因为如果有一个因子,这个数不是素数。如果对于2到sqrt(n),整数除法的余数永远不是0,那么数字是素数-all()将返回true,因为迭代中没有零。</p></li>
</ol>
<p><a href="https://docs.python.org/2/library/itertools.html#itertools.islice" rel="nofollow">https://docs.python.org/2/library/itertools.html#itertools.islice</a></p>