且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

素数算法

更新时间:2023-02-26 17:33:49

您需要创建布尔大如你想找到的最大素数的数组。在开始的时候是完全初始化为true。

You need to create an array of booleans as big as the maximum prime number you want to find. At the beginning it's completely initialized to true.

个这样的阵列单元将是真实的,如果是一个素数,还是假的,如果是不是。

The ith cell of such array will be true if i is a prime number, or false if it's not.

开始从 I = 2 迭代:它是素数,则设置为false,任何细胞具有2.进入指数多次到下一个素数( I = 3 ),做同样的。转到下一任(这是 I = 5 I = 4 不是素数:数组[4] 设置为false在处理 I = 2 ),并一次又一次地这样做。

Start iterating from i=2: it's prime, then set to false any cell with an index multiple of 2. Go to the next prime number (i=3) and do the same. Go to the next prime (it's i=5: i=4 is not prime: array[4] was set to false while processing i=2) and do the same again and again.