且构网

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

有没有一种简单的算法可以确定X是否为素数?

更新时间:2023-02-26 18:13:33

第一个算法非常好,在Project上使用了很多欧拉。如果您知道所需的最大数目,也可以研究Eratosthenes的筛子。

The first algorithm is quite good and used a lot on Project Euler. If you know the maximum number that you want you can also research Eratosthenes's sieve.

如果维护素数列表,您还可以优化第一个算法以仅用素数除直到数字的平方根。

If you maintain the list of primes you can also refine the first algo to divide only with primes until the square root of the number.

使用这两个算法(除法和筛分法),您应该可以解决问题。

With these two algoritms (dividing and the sieve) you should be able to solve the problems.

编辑:评论中指出的固定名称

Edit: fixed name as noted in comments