且构网

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

需要帮助优化算法 - 2000000下的所有质数之和

更新时间:2023-11-24 08:36:10

通过 I 2 2000000 (或任何上限),一旦你确定 I 为素数,你后来才知道, {2I,3I,4I,...,NI} 不是素数,其中 NI< = 2000000

With i from 2 to 2000000 (or whatever upper limit), once you determine that i is prime, you then know that {2i, 3i, 4i, ..., ni} are not prime numbers, where ni <= 2000000.

您不必测试这些数字 - 你知道他们是不是素数

You don't need to test those numbers — you know they aren't prime.

当你通过你的 testNumbers 阵列进步和消除这个列表的数字,数字,你知道现在是不是素数,则显著减少你真正需要测试的数字

As you progress through your array of testNumbers and eliminate numbers from this list, numbers that you now know are not prime, you significantly reduce the numbers you actually need to test.

是的,这是埃拉托色尼的筛。

Yes, this is the Sieve of Eratosthenes.