且构网

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

Python while 循环查找素数

更新时间:2022-11-15 11:29:20

您需要向上包括平方根.否则,您的算法将错过素数平方族(9、25 和 49 是素数平方).

You need to go up to and including the square root. Otherwise your algorithm will miss the family of prime squares (9, 25, and 49 are prime squares).

快速解决方法是将 < 替换为 <= 作为停止条件.

The quick fix is to replace < with <= as your stopping condition.

但考虑将停止条件改为

primes[i] * primes[i]

通过此测试,您不会进入和退出浮点数.

With this test, you don't dip in and out of floating point.