埃拉托斯特尼筛法

埃拉托斯特尼筛法希臘語英語: ),簡稱,也称素数筛。这是一種簡單且历史悠久的筛法,用來找出一定範圍內所有的質數

所使用的原理是從2開始,將每個質數的各個倍數,標記成合數。一個質數的各個倍數,是一個差為此質數本身的等差數列。此為這個篩法和試除法不同的關鍵之處,後者是以質數來測試每個待測數能否被整除。

埃拉托斯特尼篩法是列出所有小質數最有效的方法之一,其名字來自於古希臘數學家埃拉托斯特尼,並且被描述在另一位古希臘數學家尼科馬庫斯所著的《算術入門》中。[1]

算式

给出要筛数值的范围n,找出以内的。先用2去筛,即把2留下,把2的倍数剔除掉;再用下一個質數,也就是3筛,把3留下,把3的倍数剔除掉;接下去用下一個質數5筛,把5留下,把5的倍数剔除掉;不斷重複下去......。

步驟

埃拉托斯特尼筛法

详细列出算法如下:

  1. 列出2以後的所有序列:
    • 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
  2. 标出序列中的第一个質数,也就是2,序列变成:
    • 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
  3. 将剩下序列中,劃摽2的倍数(用红色标出),序列变成:
    • 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
  4. 如果现在这个序列中最大数小于等于最後一個標出的質數的平方,那么剩下的序列中所有的数都是質数,否则回到第二步。

  1. 本例中,因为25大于2的平方,我们返回第二步:
  2. 剩下的序列中第一个質数是3,将主序列中3的倍数划出(藍色),主序列变成:
    • 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
  1. 我们得到的質数有:2,3
  2. 25仍然大于3的平方,所以我们还要返回第二步:
  3. 现在序列中第一个質数是5,同样将序列中5的倍数划出(綠色),主序列成了:
    • 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
  4. 我们得到的質数有:2, 3, 5 。
  5. 因为25等于5的平方,結束循环

结论:去掉红色的数字,2到25之间的質数是:2, 3, 5, 7, 11, 13, 17, 19, 23。

演算法

埃拉托斯特尼篩法,可以用以下的來表示:

Input: an integer n > 1
 
Let A be an array of Boolean values, indexed by integers 2 to n,
initially all set to true.
 
 for i = 2, 3, 4, ..., not exceeding :
  if A[i] is true:
    for j = i2, i2+i, i2+2i, i2+3i, ..., not exceeding n:
      A[j] := false
 
Output: all i such that A[i] is true.

以上演算法可以得到小於等於n的所有,它的複雜度是O(n log log n)

程式代码

Python 3.6

def eratosthenes(n):
    IsPrime = [True] * (n + 1)
    for i in range(2, int(n ** 0.5) + 1):
        if IsPrime[i]:
            for j in range(i * i, n + 1, i):
                IsPrime[j] = False
    return [x for x in range(2, n + 1) if IsPrime[x]]
if __name__ == "__main__":
    print(eratosthenes(120))

C++

#include <vector>
#include <cmath>

auto eratosthenes(int upperbound) {
  std::vector<bool> flag(upperbound + 1, true);
  flag[0] = flag[1] = false; //exclude 0 and 1
  for (int i = 2; i <= sqrt(upperbound); ++i) {
    if (flag[i]) {
      for (int j = i * i; j <= upperbound; j += i)
        flag[j] = false;
    }
  }	
  return flag;
}

R

eratosthenes <- function(n) {
  if (n == 1) return(NULL)
  if (n == 2 | n == 3) return(2:n)
  numbers <- 2:n
  primes <- rep(TRUE, n-1)
  for (i in 2:floor(sqrt(n))) {
    if (primes[i-1]) {
      for (j in seq(i * 2, n, i))
        primes[j-1] <- FALSE
    }
  }
  return(numbers[primes])
}

JavaScript

var countPrimes = function(n) {
    let count = 0;
    let signs = new Uint8Array(n);

    for (let i = 2; i <= n; i++) {
        if (!signs[i - 1]) {
            count++;

            for (let j = i * i; j <= n; j += i) {
                signs[j - 1] = true;
            }
        }
    }
    return count;
};

参见

参考文献

  1. Nicomachus, Introduction to Arithmetic, I, 13.
  • or, The Sieve of Eratosthenes. Being an Account of His Method of Finding All the Prime Numbers, by the Rev. Samuel Horsley, F. R. S., Philosophical Transactions (1683-1775), Vol. 62. (1772), pp. 327-347.

拓展阅读

This article is issued from Wikipedia. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.