求质数

Algorithm

prime number is a natural number which has exactly two distinct natural number divisors1and itself.

To find all the prime numbers less than or equal to a given integer n by Eratosthenes' method:

  1. Create a list of consecutive integers from two to n: (2, 3, 4, ..., n).
  2. Initially, let p equal 2, the first prime number.
  3. Strike from the list all multiples of p less than or equal to n. (2p, 3p, 4p, etc.)
  4. Find the first number remaining on the list after p (this number is the next prime); replace p with this number.
  5. Repeat steps 3 and 4 until p2 is greater than n.
  6. All the remaining numbers in the list are prime.

[edit]Example

To find all the prime numbers less than or equal to 30, proceed as follows:

First generate a list of integers from 2 to 30:

 2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

Strike (sift out) the multiples of 2 resulting in:

 2  3     5     7     9    11    13    15    17    19    21    23    25    27    29

The first number in the list after 2 is 3; strike the multiples of 3 from the list to get:

 2  3     5     7          11    13          17    19          23    25          29

The first number in the list after 3 is 5; strike the remaining multiples of 5 from the list:

 2  3     5     7          11    13          17    19          23                29

The first number in the list after 5 is 7, but 7 squared is 49 which is greater than 30 so the process is finished. The final list consists of all the prime numbers less than or equal to 30.

[edit]Algorithm complexity and implementation

The crossing-off of multiples of each found prime number can be started at the square of the number, as lower multiples have already been crossed out during the previous steps.

The complexity of the algorithm is O(n(logn)(loglogn)) bit operations with a memory requirement of O(n).[4] Time complexity in RAM machine model is O(nloglogn) operations; this is a direct consequence of the fact that the prime harmonic series asymptotically approaches 1/(ln(ln(N))). The segmented version of the sieve of Eratosthenes, with basic optimizations, uses O(n) operations and O(n1 / 2loglogn / logn) bits of memory.[5]

David Turner suggested in 1975 that the primes sieve could be represented in a strikingly simple and elegant way in purely functional programming languages.[6] Turner's sieve, which is more closely related to the Euler's sieve below, rendered in Haskell, is:

 primes = sieve [2..]
 sieve (p : xs) = p : sieve [x | x <- xs, x `mod` p > 0]

In 2008, Melissa O'Neill showed that the complexity of Turner's algorithm is significantly worse than the complexity of the classic imperative renditions of the sieve.[7]O'Neill demonstrated a priority queue based rendition of the sieve of Eratosthenes in Haskell with complexity similar to that of the classic imperative implementations.

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值