Siekin of Atkin解释

时间:2009-06-21 12:13:35

标签: primes sieve-of-eratosthenes sieve-of-atkin

我正在做一个项目,我需要一种有效的方法来计算素数。我使用了sieve of Eratosthenes,但我一直在搜索,发现sieve of Atkin是一种更有效的方法。我发现很难找到这种方法的解释(我能够理解!)。它是如何工作的?示例代码(最好是在C或python中)很棒。

编辑:感谢您的帮助,我唯一不理解的是x和y变量在伪代码中引用的内容。有人可以帮我解释一下吗?

5 个答案:

答案 0 :(得分:13)

wiki page始终是一个很好的起点,因为它完整地解释了算法并提供了注释的伪代码。 (N.B.有很多细节,因为维基网站可靠起来,我不会在这里引用它。)

对于您提到的特定语言的参考文献:

希望有所帮助。

答案 1 :(得分:10)

Wikipedia article有一个解释:

  • "算法完全忽略任何可被2,3或5整除的数字。具有偶数模60的余数的所有数字都可以被2整除,而不是素数。模数为60的余数除以3的所有数字也可被3除尽,而不是素数。模数为60的余数除以5的所有数字都可以被5整除,而不是素数。所有这些遗留物都被忽略了。"

让我们从着名的

开始吧
primes = sieve [2..] = sieve (2:[3..])   
  -- primes are sieve of list of 2,3,4... , i.e. 2 prepended to 3,4,5...
sieve (x:xs) = x : sieve [y | y <- xs, rem y x /= 0]   -- set notation
  -- sieve of list of (x prepended to xs) is x prepended to the sieve of 
  --                  list of `y`s where y is drawn from xs and y % x /= 0

让我们看看它是如何进行的几个第一步:

primes = sieve [2..] = sieve (2:[3..]) 
                     = 2 : sieve p2     -- list starting w/ 2, the rest is (sieve p2)
p2 = [y | y <- [3..], rem y 2 /= 0]     -- for y from 3 step 1: if y%2 /= 0: yield y

p2不包含 2 的倍数。 IOW它只包含与 2 相互作用的数字 - p2中没有数字 2 作为其因素。要查找p2,我们实际上并不需要在[3..](&#39; s 3 3 中按 2 来测试除以up, 3,4,5,6,7,... ),因为我们可以提前枚举 2 的所有倍数:

rem y 2 /= 0  ===  not (ordElem y [2,4..])     -- "y is not one of 2,4,6,8,10,..."

ordElemelem类似(即 is-element 测试),只是假设其列表参数是有序的,增加的列表数字,所以它可以安全地检测不存在以及存在:

ordElem y xs = take 1 (dropWhile (< y) xs) == [y]   -- = elem y (takeWhile (<= y) xs) 

普通的elem并不假设任何东西,因此必须检查其列表参数的每个元素,因此无法处理无限列表。 ordElem可以。那么,那么,

p2 = [y | y <- [3..], not (ordElem y [2,4..])]  -- abstract this as a function, diff a b =
   = diff      [3..]                 [2,4..]    --       = [y | y <- a, not (ordElem y b)]
   -- 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
   -- . 4 . 6 . 8 . 10  . 12  . 14  . 16  . 18  . 20  . 22  .
   = diff [3..] (map (2*)            [2..] )  --  y > 2, so [4,6..] is enough
   = diff [2*k+x | k <- [0..],  x <- [3,4]]   -- "for k from 0 step 1: for x in [3,4]:
          [2*k+x | k <- [0..],  x <- [  4]]   --                            yield (2*k+x)"
   = [     2*k+x | k <- [0..],  x <- [3  ]]   -- 2 = 1*2 = 2*1
   = [3,5..]                                  -- that's 3,5,7,9,11,...

p2只是 2 以上所有赔率的列表。好吧,我们知道 。那下一步呢?

sieve p2 = sieve [3,5..] = sieve (3:[5,7..]) 
                         = 3 : sieve p3
p3 = [y | y <- [5,7..], rem y 3 /= 0]
   = [y | y <- [5,7..], not (ordElem y [3,6..])]           -- 3,6,9,12,...
   = diff [5,7..] [6,9..]         -- but, we've already removed the multiples of 2, (!)
   -- 5 . 7 . 9 . 11 . 13 . 15 . 17 . 19 . 21 . 23 . 25 . 27 .
   -- . 6 . . 9 . . 12  . . 15 . . 18  . . 21 . . 24  . . 27 .
   = diff [5,7..] (map (3*) [3,5..])                       -- so, [9,15..] is enough
   = diff [2*k+x | k <- [0..], x <- [5]] (map (3*)
          [2*k+x | k <- [0..], x <- [    3]] )
   = diff [6*k+x | k <- [0..], x <- [5,7,9]]               -- 6 = 2*3 = 3*2
          [6*k+x | k <- [0..], x <- [    9]] 
   = [     6*k+x | k <- [0..], x <- [5,7  ]]               -- 5,7,11,13,17,19,...

接下来,

sieve p3 = sieve (5 : [6*k+x | k <- [0..], x <- [7,11]])
         = 5 : sieve p5
p5 = [y | y <-        [6*k+x | k <- [0..], x <- [7,11]], rem y 5 /= 0]
   = diff [ 6*k+x | k <- [0..], x <- [7,11]]   (map   (5*)
          [ 6*k+x | k <- [0..], x <- [                  5,       7]]) -- no mults of 2 or 3!
   = diff [30*k+x | k <- [0..], x <- [7,11,13,17,19,23,25,29,31,35]]  -- 30 = 6*5 = 5*6
          [30*k+x | k <- [0..], x <- [                 25,      35]]
   = [     30*k+x | k <- [0..], x <- [7,11,13,17,19,23,   29,31   ]]

这是阿特金筛子的工作顺序。其中不存在 2,3 5 的倍数。 Atkin和Bernstein的工作模拟 60 ,即它们的范围加倍:

p60 = [ 60*k+x | k <- [0..], x <- [1, 7,11,13,17,19,23,29,31, 37,41,43,47,49,53,59]]

接下来,他们使用一些复杂的定理来了解每个数字的某些属性,并相应地处理每个数字。整个过程重复(a-la the&#34; wheel&#34;),周期 60

  • &#34;所有数字n的模数为60的余数1,13,17,29,37,41,49或53(...)是素数,当且仅当解的数量为4x^2 + y^2 = n是奇数,数字是无广的。&#34;

这是什么意思?如果我们知道该等式的解的数量对于这样的数字是奇数,那么它是素数如果它是无平方的。这意味着它没有重复的因素(如 49,121,等)。

Atkin和Bernstein使用它来减少整体操作次数:对于每个素数(从 7 及以上),我们枚举(并标记为删除)其正方形的倍数< / em>,所以它们比Eratosthenes的筛子更远,因此在给定的范围内它们更少。

其余规则是:

  • &#34;当且仅当3x^2 + y^2 = n的解数为3x^2 − y^2 = n时,所有具有模数为60的余数7,19,31或43(...)的数n为素数是奇数,数字是无平方。&#34;

  • &#34;当且仅当p60的解的数量为时,所有具有模数为60的余数11,23,47或59(...)的数n为素数是奇数,数字是无平方。&#34;

这将处理log log N定义中的所有16个核心数字。

另见:Which is the fastest algorithm to find prime numbers?


Eratosthenes筛子在生产 N 之前的时间复杂度是 O(N log log N),而Atkin筛子的时间复杂度是 > O(N)(将额外的{{1}}优化放在一边,这些优化不能很好地扩展。然而,公认的智慧是阿特金筛子中的常数因子要高得多,所以它可能只能高于32位数( N> 2 32 if at all

答案 2 :(得分:3)

  

编辑:我唯一不理解的是x和y变量在伪代码中引用的内容。有人可以帮我解释一下吗?

有关维基百科页面伪代码(或Atkin的Sieve的其他更好实现)中常见使用'x'和'y'变量的基本解释,您可能会发现my answer to another related question有用。

答案 3 :(得分:2)

这是一个可以帮助你的atkins筛子的c ++实现......

#include <iostream>
#include <cmath>
#include <fstream>
#include<stdio.h>
#include<conio.h>
using namespace std;

#define  limit  1000000

int root = (int)ceil(sqrt(limit));
bool sieve[limit];
int primes[(limit/2)+1];

int main (int argc, char* argv[])
{
   //Create the various different variables required
   FILE *fp=fopen("primes.txt","w");
   int insert = 2;
   primes[0] = 2;
   primes[1] = 3;
   for (int z = 0; z < limit; z++) sieve[z] = false; //Not all compilers have false as the       default boolean value
   for (int x = 1; x <= root; x++)
   {
        for (int y = 1; y <= root; y++)
        {
             //Main part of Sieve of Atkin
             int n = (4*x*x)+(y*y);
             if (n <= limit && (n % 12 == 1 || n % 12 == 5)) sieve[n] ^= true;
             n = (3*x*x)+(y*y);
             if (n <= limit && n % 12 == 7) sieve[n] ^= true;
             n = (3*x*x)-(y*y);
             if (x > y && n <= limit && n % 12 == 11) sieve[n] ^= true;
        }
   }
        //Mark all multiples of squares as non-prime
   for (int r = 5; r <= root; r++) if (sieve[r]) for (int i = r*r; i < limit; i += r*r) sieve[i] = false;
   //Add into prime array
   for (int a = 5; a < limit; a++)
   {
            if (sieve[a])
            {
                  primes[insert] = a;
                  insert++;
            }
   }
   //The following code just writes the array to a file
   for(int i=0;i<1000;i++){
             fprintf(fp,"%d",primes[i]);
             fprintf(fp,", ");
   }

   return 0;
 }

答案 4 :(得分:1)

// Title : Seive of Atkin ( Prime number Generator) 

#include <iostream>
#include <cmath>
#include <vector>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    long long int n;
    cout<<"Enter the value of n : ";
    cin>>n;
    vector<bool> is_prime(n+1);
    for(long long int i = 5; i <= n; i++)
    {
        is_prime[i] = false;
    }
    long long int lim = ceil(sqrt(n));

    for(long long int x = 1; x <= lim; x++)
    {
        for(long long int y = 1; y <= lim; y++)
        {
            long long int num = (4*x*x+y*y);
            if(num <= n && (num % 12 == 1 || num%12 == 5))
            {
                is_prime[num] = true;
            }

            num = (3*x*x + y*y);
            if(num <= n && (num % 12 == 7))
            {
                is_prime[num] = true;
            }

            if(x > y)
            {
                num = (3*x*x - y*y);
                if(num <= n && (num % 12 == 11))
                {
                    is_prime[num] = true;
                }
            }
        }
    }
    // Eliminating the composite by seiveing
    for(long long int i = 5; i <= lim; i++)
    {
        if(is_prime[i])
            for(long long int j = i*i; j <= n; j += i)
            {
                is_prime[j] = false;
            }
    }
    // Here we will start printing of prime number
   if(n > 2)
   {
       cout<<"2\t"<<"3\t";
   }
   for(long long int i = 5; i <= n; i++)
   {
         if(is_prime[i])
         {
             cout<<i<<"\t";
         }
    }
    cout<<"\n";
    return 0;
}