擅长:python、mysql、java
<p>你可以试试埃拉托斯泰尼的方法。取一个数组,其中包含所有需要检查升序的数字,转到数字2并标记它。现在每秒钟抓取一个数字直到数组的末尾。然后转到3并标记。在那之后,每三个数字都被划伤一次。然后转到4-它已经被划伤了,所以跳过它。对尚未划伤的n+1重复此操作。在</p>
<p>最后,标记的数字是素数。该算法速度较快,但有时需要大量内存。您可以通过删除所有偶数(因为它们不是素数)并手动在列表中添加2来优化它。这会稍微扭曲逻辑,但会占用一半的内存。在</p>
<p>下面是我所说的一个例子:<a href="http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes" rel="nofollow">http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes</a></p>