Prime Number Sieve (Linear Sieve Method)

Table of contents

Article directory

foreword

1. What is the linear sieve method?

2. What is the principle?

1. Minimum prime number

2. Eliminate non-prime numbers

3. How to ensure that non-prime numbers are not repeatedly eliminated?

3. Code implementation


foreword

Introduce the theoretical basis of the linear sieve method and its code implementation


Tip: The following is the text of this article, and the following cases are for reference

1. What is the linear sieve method?

The linear sieve method refers to screening out all prime numbers of a given number under the complexity of O(n)

2. What is the principle?

1. Minimum prime number

It can be seen from the axiom that n can be decomposed into the product of multiple prime numbers, then, among these prime numbers, we can always find a smallest number x, then x is the smallest prime number of n

2. Eliminate non-prime numbers

Now that we have the smallest prime number x, for each number, we only need to start from 2 and traverse 2-n one by one (assuming that the number we traversed at this time is i), if we find that i%x=0,

Then we know that this number should be eliminated because it can be represented by the product of at least two numbers (does not meet the definition of a prime number)

3. How to ensure that non-prime numbers are not repeatedly eliminated?

We know that 45 can be 3*15, or 5*9. If we follow the idea of ​​the second step, we must repeatedly screen out 45 twice. If n is large enough, there will be many repeated operations, which is impossible accepted

So what should we do?

It's very simple, we just need to ensure that 45 is screened out by its smallest prime number (3) as we said at the beginning, so what should we do?

if(i%pri[j]==0)break;

What is the meaning of this sentence? Let's make an analogy, if i = 4 at this time , that is, 12=2*2*3=3*4 , we need to let 12 be screened out by 2

Then when pri[j]=2 , we have to stop this for loop, why do we do this?

vis[pri[j]*i]=1;

It's very simple, because i can be 2 at this time ( i=12=2*2*3 ), then can we assert that x =  pri[j] * i = pri[j] * 2 * k

 (That is to say, the minimum prime number of x is 2) can also berepresented by 2 ?

The answer is yes, from which we can infer that if we do not break at this time, we will screen out 12 in this cycle (3*4, 12 will be screened out through 3)

But in the next cycle, we will screen out 12 through 2*6, which is obviously not in line with our purpose, to ensure that each number is screened once, so we only need

Ensure that each number is screened out by its own smallest prime number, in other words, when i is divisible by the current prime number (the current integer is the smallest prime number of i) ,

Just break, so that the following numbers (all subsequent numbers containing i will not be screened out by the current cycle) will not be screened out repeatedly.


3. Code implementation

int pri[N+9>>1],now;
bool vis[N+9];
void init(){
    for(int i=2;i<=N;i++){
        if(!vis[i])pri[++now]=i;
        for(int j=1;j<=now&&pri[j]*i<=N;j++){
            vis[pri[j]*i]=1;
            if(i%pri[j]==0)break;
        }
    }
}

Guess you like

Origin blog.csdn.net/Wansit/article/details/115439545