Determine the number and quality of screening

Knowledge prime number

Benpian essays explain Olympiad in Informatics relevant content related to mathematical knowledge in prime numbers, including screening and general method for determining prime numbers. Readers need to have a basic knowledge of mathematics literacy of not less than a high school herein, this will no longer essays on basic knowledge, concepts and symbols mathematics to explain. There is a need tutoring their own students.

Class! !

1, the quality and definition of the basic concepts

Definition: If a positive integer not divisible by any number except 1 and of its own, called the prime number (or prime). Otherwise, it said the number is a composite number.

We need to be clear, the entire set of natural numbers, the distribution of prime numbers sparse, for a sufficiently large integer \ (N \) , does not exceed its prime number about \ (\ frac {N} { ln N} \) a, in other words, that is, each \ (ln N \) number approximately a prime number.

2, the determined prime number (trial division)

We need to be clear, the best part is that the computer mechanically repeat the same thing, for the determination of prime numbers, happens to be its "strengths", we just need to start by definition, to enumerate all the numbers from 2 to n, if divisible it returns false, not true, then return it to the final test.

template:

bool prime(int n)
{
    for(int i=2;i<n;i++)
        if(n%i==0)
            return false;
    return true;
}

However, this method would waste a lot of time, if the program needs to call this function sentenced to prime several times, it will greatly increase the time complexity of the algorithm. So we used the template is an optimized version(Think of it)

Us to think that if this number \ (N \) can be a number \ (a \) divided by (less than this number of course it will), it must also have another number \ (b \) can be divisible by it, and \ (b \ cdot a \) must be equal to \ (N \) .
Therefore, we only need to enumerate from 2 \ (\ sqrt {N} \ ) can.
template:

bool prime(int n)
{
    for(int i=2;i<=sqrt(n);i++)
        if(n%i==0)
            return false;
    return true;
}

3, screened prime numbers

Screening of prime numbers is given number \ (N \) , asked to identify from 1 to \ (N \) all prime numbers. Screening of prime numbers is very important letter competition in research, but also some important model-based problem solving math problems.

(1)Eratosthenes筛法

Eratosthenes sieve method that is Ace Torah Courtenay sieve, sieve method referred to Erichsen. It is based on this idea:
a multiple of any integer is not a prime number is
based on the idea, we start from small to large 2 enumerate each number \ (the X-\) , and it is less than \ (N \) is a multiple of all marked mark, when we scanned a number of times, if it is not marked, it is a prime number.
This screening method excellent understanding, but less efficient, we can take a pen and paper simulation, then you can find that there are some number is a multiple of the number of more quality, so it will be repeated several times the screen (for example, 6) so although not affect the correct answer, but it affects the efficiency of the program running. Therefore, we optimized Eppendorf sieve method:
For each number \ (X \) , we have from \ ({x ^ 2} \ ) start flag, the \ ({x ^ 2} \ ) , \ ((X + . 1) \ CDOT X \) , \ (\ cdots \) , \ (\ X FRAC {{N}} \ CDOT X \) marking can.

Optimized template:

void Estorathenes(int n)
{
    memset(v,0,sizeof(v));
    cnt=0;
    for(int i=2;i<=n;i++)
    {
        if(v[i])
            continue;
        prime[++cnt]=i;
        for(int j=i;j<=n/i;j++)
            v[i*j]=1;
    }
}

(2) Euler sieve

I.e. Euler Euler sieve sieve method, also known as rapid screening, linear sieve method. Jing letter is the most commonly used of prime number sieve method to the great mathematician Euler named, suggest that you master.
Erichsen sieve method described above will be repeated even after the optimized mark, having been repeated a lot less, but still not fast enough. For example, only 12 will be 2 mark 3 mark will be, because 12 = 6 2 = 4 3. Why does this happen? Because we screened according to Erichsen sieve, and did not find out the only way to determine 12, 12 iterate inevitable lead to duplication.
According to this basis, there has been rapid screening algorithm, its basic idea is this: we need to generate a marked when combined number, each only take a minimum quality factor on the closing number to an existing number, so as a result, the number of co-prime factors will be small to add up the earth, and that is just 12, it was the only break down into three 2 2
The time complexity of this algorithm is \ (O (N) \) a.

Euclidean sieve method template:

void Euler(int n)
{
    memset(v,0,sizeof(v));//v[i]表示i的最小质因子
    cnt=0;
    for(int i=2;i<=n;i++)
    {
        if(!v[i])
            v[i]=prime[++cnt]=i;
        for(int j=1;j<=cnt;j++)
        {
            if(prime[j]>v[i] || prime[j]>n/i)
                break;
            v[i*prime[j]]=prime[j];
        }
    }
}

4, the prime factorization of

The only decomposition theorem integer

This theorem hin important !!!

Unique Factorization Theorem integers: any integer greater than 1 can be expressed as the product of a number of prime numbers, as follows:
\ [{N} = {{P_1} ^ {c_1 and}} \ CDOT {{P_2} ^ {c_2} } \ cdot \ cdots \ cdot { {p_m} ^ {c_m}} \]

Therefore, we analyzed by the above theorem of prime factorization of an implementation on a computer:
start with the 2 \ (\ sqrt {N} \ ) scans all integers \ (X \) , if the \ (X \) divisible \ (N \ ) , then add it in the factor table and by \ (N \) has in addition to \ (X \) , each index table in addition to c ++ array, in addition to up to up different.

Code:

void divide(int n)
{
    cnt=0;
    for(int i=2;i<=sqrt(n);i++)
    {
        if(n%i==0)
        {
            prime[++cnt]=i;
            c[cnt]=0;
        }
        while(n%i==0)
            n/=i,c[cnt]++;
    }
    if(n>1)
        prime[++cnt]=n,c[cnt]=1;
}

Guess you like

Origin www.cnblogs.com/fusiwei/p/11354110.html