Prime 筛选

classic:

int isprime(int n)
{
  int i;
  if(n<=1) return 0;
  for(i=2;i<=sqrt(n);i++)
    if(n%i==0) return 0;
  return 1;
}

Obviously if you want to determine prime numbers within a certain range, this algorithm is very slow.

Eratosthenes (Eratosthenes) sieve

int flag[maxn+5]={1,1};  //if(flag[i]=0) i为素数
void isprime()
{
  int i,j;
  memset(flag,0,sizeof(flag));
  for(i=2;i<=sqrt(N);i++)
  {
    if(!flag[i])
    {
      for(j=i*i;j<=N;j+=i)
        flag[j]=1;
    }
  }
}

Euler (Euler) sieve

int flag[maxn+5]={1,1},prime[maxn+5];  //if(flag[i]=0) i为素数
void isprime()
{
  int i,j,cnt=0;
  memset(flag,0,sizeof(flag));
  for(i=2;i<=maxn;i++)
  {
    if(!flag[i])  prime[cnt++]=i;
    for(j=0;j<cnt&&prime[j]*i<=maxn;j++)
    {
      flag[prime[j]*i]=1;
      if(i%prime[j]==0) break;
    }
  }
}

Why when i | when prime [j], will be out of the loop?

, So that i | prime [j] = m , it can be seen that i * prime [j + 1] can be obtained by the k '* prime [j], and because the prime [j + 1]> prime [j], so k 'at that time is certainly greater than i, that is i * prime [j + 1] may be updated in the future, do so at this time, in order to avoid double counting.

Interval prime number sieve

 

Cited: Given integers a and b, may I ask the interval [a, b) how many prime numbers? (A <b≤10 ^ 12, b

Because the minimum number of prime factors must not exceed engagement within √b b, d because if there is a divisor of n, then n / d n is a submultiple of the n = d × n / d understood min (d, n / d) ≤√n.

If there is less than √n prime number table, then it can be used in Angstroms sieve of formula [a, b) on. That is, to each well [2, √b) tables and [a, B) a table, and then from [2, √b) Table sieved prime number, but also from its multiples [a, b) filtering out of the table, it is the last remaining prime interval [a, b) in the.

If the requirements [1e ^ 9,1e ^ 9 + 2) prime numbers in the interval, are we going to open array 1e ^ 9 + 3 size of it? In fact, do not use, we use the subscript offset can reduce the space overhead. Is about [1e ^ 9,1e ^ 9 + 2) corresponds to [0,2), the whole interval offset to the left 1e ^ 9.

Plainly, it is to use a known small primes update unknown wide range of prime numbers, and then use the index offset maintenance interval .

Board questions poj 2689 Prime Distance

#include <bits / STDC ++ H.>
 the using  namespace STD; 
typedef Long  Long LL;
 const  int MAXN = 1E5;
 BOOL is_prime [MAXN], is_prime_small [MAXN]; 
LL Prime [MAXN], prime_num = 0 ;
 // for the interval [ a, b in an integer) performing sieve method, is_prime [ia] = true <=> i is represented by a prime number (offset subscript a) 
void segment_sieve (LL a, b LL) 
{ 
  for (i LL = 0 ; i * I <B; I ++) is_prime_small [I] = to true ; // to [2, sqrt (b)) is initialized are all prime numbers 
  for (LL I = 0 ; I <BA; I ++) is_prime [I] = to true ; //On [a, b subscript after the shift) is initialized 
  for (LL = I 2 ; I * I <B; I ++) // filter [2, sqrt (B)) 
  { 
     IF (is_prime_small [I]) 
    { 
      for (LL J = 2 * i; J * J <B; = J + i) 
        is_prime_small [J] = to false ; 
      
      // (. 1-a + i) / i i give the nearest multiple of a minimum of 2 i is fold, then screened 
      for (LL J = max (2LL, (A + I- . 1 ) / I) I *; J <B; = J + I) 
        is_prime [J -a] = to false ; 
    } 
  } 
  for (I = LL 0 ; I <BA; I ++) // count the number of index offset 
    if (is_prime [I])
      prime[prime_num++]=i+a;
}
int main() 
{
  ll a,b;
  while (~scanf("%lld%lld",&a,&b)) 
  {
    prime_num=0;
    memset(prime,0,sizeof(prime));
    segment_sieve(a,b);
    printf("%lld\n",prime_num);
  }
  return 0;
}

Guess you like

Origin www.cnblogs.com/VividBinGo/p/11388481.html