Study Notes prime number

definition

If a natural number can not be any natural number divisible by 1 and in addition to its own, this number is called a prime number (also known prime number).
Throughout the set of natural numbers, the distribution of small primes, sparse, for a sufficiently large natural number N, N is a prime number not more than about N / lnN number, i.e. number per lnN Approximately a prime number.

Determination of prime numbers

Trial division

   If a positive integer is a composite number, there is a number divisible by N T, where 2 $ \ leq $ T $ \ leq $ N. It proved a little of it.
According to the above proposition, we only need to scan 2 ~ $ \ sqrt {N} $ between all integers, N is divisible by sequentially checking whether they can, if are not divisible, then N is prime, or a composite number. The time complexity is O ($ \ sqrt {N} $).

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

There is also a Miller-Robbin determination method, a stochastic algorithm is more efficient, I'll go to school.

Screening of prime numbers

Here are a few methods of screening for prime numbers.

Eratosthenes筛法

Multiples of any integer x, are certainly not a prime number.
We can start from 2, each scan a small to large number of multiple scans then this number, a mark is a composite number, when scanning a number, if it is not labeled, it can not be a 2 ~ N-1 divisible, it is a prime number.
There is less than $ x ^ 2 $ number has been marked over a smaller number, so we can optimize, from $ x ^ 2 $ mark the beginning of multiples.

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

Eppendorf sieve time complexity is O (NloglogN).

Linear sieve

Linear sieve method is a better method than Eppendorf sieve sieve. It is through the " small to large prime factors accumulation " of each composite number is labeled, so that the number of marking further compressed together, improve efficiency. V per array provided the minimum number of recording quality factor, the following steps maintenance v:

  1. Sequentially considering each number i between 2 ~ N.
  2. If v [i] = i, i is a prime number described, to save it.
  3. Scan is not larger than v [i] for each prime number p, so that v [i $ \ times $ p ] = p. I.e. a quality factor p accrue based on the re-i. Since p $ \ leq $ v [i ], it is the smallest prime factors p composite number p $ \ times $ i's.
    The time complexity of O (N).
void primes(int n)
{
    memset(v,0,sizeof(v)); //最小质因子
    m=0; //质数数量
    for (int i = 2; i <= n; i++)
    {
        if (v[i]==0)
        {
            v[i]=i;
            prime[++m]=i;  //i是质数
        }
        for (int j = 0; j <= m; j++)
        {
            if(prime[j]>v[i]||prime[j]>n/i) break;  //质数的范围不能超出v[i]和n
            v[i*prime[j]]=prime[j];
        }
    }
}

Prime factorization of

Fundamental Theorem of Arithmetic

Any positive integer greater than 1, the product can be decomposed into a finite number of unique prime number, can be written as:

N=p_1^{c_1}p_2^{c_2}...p_m^{c_m}

Of which $ c_i $ are positive integers, $ p_i $ is a prime number, and satisfies:

p_1<p_2<...<p_m

Trial division

This approach combines the determination of the prime number "trial division" and "Eppendorf sieve."
Scan 2 ~ $ \ sqrt {N} $ each integer of d, d if the multiplication and division can be N, N, removed from all of the factors d, while the cumulative number of the removed d.
Because each factor is the first time to scan this factor was completely removed, so the time complexity is O (N).

void divide(int n)
{
    m=0;
    for (int i = 2; i <= sqrt(n); i++)
    {
        if (n%i==0)  //这里i一定是质数,因为后面搜到合数时,早已被前面的质数除掉了
        {
            p[++m]=i,c[m]=0;
            while(n%i==0)
            {
                n/=i;
                c[m]++;
            }
        }
    }
    if (n>1)  //n是质数
    {
        p[++m]=n;
        c[m]=1;
    }
}

There is also a " Pollard's Rho " algorithm, a competition division and efficient.

Guess you like

Origin www.cnblogs.com/zxj-hans/p/11261148.html