Number theory Linear sieve summary (prime sieve, Euler function sieve, Möbius function sieve, first n number of approximately several sieve)

Source: Internet
Author: User

Linear sieve


Linear sieve plays a very important role in number theory, can greatly reduce the time complexity of solving some problems, the use of linear sieve has a premise (except the prime sieve) the function must be defined in the number theory of the integrable function, that is, a positive integer n an arithmetic function f (n), if f (1) = 1, and when a,b coprime when F (ab) =f (a) f (b), in number theory it is called the integrable function, if a B is not coprime also satisfied is called the complete integrable function, The following shows how each sieve is sifted.

The most basic is the prime sieve, the other three sieves are based on prime sieve as the premise


Prime sieve

void Get_prime ()  {      int pnum = 0;       for (int i = 2; i < MAX; i++)      {          if (!noprime[i])              P[pnum + +] = i;          for (int j = 0; J < pnum && I * p[j] < MAX; j + +)          {              noprime[i * p[j]] = true;            if (i% p[j] = = 0) break;}}}  

Mainly break there, such as 12 of this number, in the ordinary sieve 12 to be 2 and 3 are screened once, obviously this extra operation will increase the time complexity, the linear sieve in a number is only its smallest element factor sieve off, 12 is 2 sieve off, when I equals 6 2*6==12 sieve off 12, this time 6%2 ==0 can break, if not broken, then 6 will also be 18 sieve off, at this time is through 6*3 to sift out 18, but obviously 18 the smallest factor is 2, so when I enumerated to 9 when there is 9*2==18, so 18 was sieved again, Therefore, when I equals 6, do not take 6 to sieve 18, the following formula to illustrate:

When P[j] is the factor of I, set i=p[j]*k, because the element factor from small to large enumeration, so p[j] is the minimum factor of I, at this time I have no need to remove P[j ']*i (j ' >j) Form of composite, because P[j ']*i can be written p[j ']* (p[j]*k) =p[j]* (P[j ']*k), which means that all p[j ']*i will be removed from the future of some I ' =p[j ']*k, the current I is not required.



Euler function sieve

Euler function Phi[i] represents the number of 1-i and I coprime.

Solve three kinds of situations:

1. When I is a prime number, obviously phi[i] = i-1

2. When I% p[j]! = 0 o'clock, gcd (i, p[j]) = 1, the properties of the integrable function can be phi[i * p[j]] = phi[i] * Phi[p[j]] = phi[i] * (P[j]-1) (P array represents prime numbers)

3. When I% p[j] ==0, according to the Euler function of the method: Phi[n] = n *∏ (1-1/p), p is the mass factor of N, so if I% p[j] = = 0,i * P[j] The number of mass factors unchanged

Phi[i * P[j]] = i * p[j] *∏ (1-1/p) = p[j] * I *∏ (1-1/p) = p[j] * Phi[i]

Thus the Euler function sieve is obtained:

void Get_eular ()  {      pnum = 0;    for (int i = 2; i < MAX; i++)      {          if (!noprime[i])          {              P[pnum + +] = i;              Phi[i] = i-1;          }          for (int j = 0; J < pnum && I * p[j] < MAX; j + +)          {              noprime[i * p[j]] = true;              if (i% p[j] = = 0)              {                  phi[i * p[j]] = phi[i] * P[j];                  break;              }              Phi[i * P[j]] = phi[i] * (P[j]-1);          }      }  


Möbius function sieve

Möbius function Mob[i]

If I is an odd number of different primes of the product mob[i] = 1

If I is an even number of different primes of the product mob[i] = 1

If I has a square factor then mob[i] = 0.

This is easier than Euler's function, on the prime sieve, if I is a prime number mob[i] = 1, if I% p[j] = = 0, then Mob[i * p[j]] = 0, obviously P[j] is its square factor, otherwise mob[i * p[j]] =-mob[i]

Thus the Möbius function sieve is obtained:

void Mobius () {    int pnum = 0;    MOB[1] = 1;    for (int i = 2; i < MAX; i++)    {        if (Noprime[i])        {            P[pnum + +] = i;            Mob[i] =-1;        }        for (int j = 0; J < pnum && I * p[j] < MAX; j + +)        {            noprime[i * P[j]] = false;            if (i% p[j] = = 0)            {                mob[i * p[j]] = 0;                break;            }            Mob[i * P[j]] =-mob[i];}}    }


The number of the first n number of the sieve

Facnum[i] = Approximately several numbers of I

It is very ingenious to get the approximate number of the first n number by the prime sieve, first according to the theorem of approximate numbers:

for a greater than 1 positive integer n can decompose factorization:The number of positive approximations of N is:
We need an auxiliary array d[i], which represents the sub-power of the minimum quality factor of I, (the smallest reason is that the prime sieve is screened composite with the smallest mass factor each time), or three cases:
1. When I is a prime number, facnum[i] = 2;d[i] = 1, well understood
2. When I% p[j]! = 0 o'clock, gcd (i, p[j]) = 1, the property of the integrable function can be facnum[i * p[j]] = facnum[i] * Facnum[p[j]] = facnum[i] * 2
D[i * P[j]] = 1 (no square factor)
3. When I% p[j] = = 0 o'clock, the square factor appears, the minimum quality factor of Cestiga 1, so there is facnum[i * p[j]] = facnum[i]/(D[i] + 1) * (D[i] + 2)
D[i * P[j]] = D[i] + 1
This results in an approximate number of the first n numbers of sieves:

void Get_facnum () {    int pnum = 0;    FACNUM[1] = 1;    for (int i = 2; i < MAX; i++)    {        if (!noprime[i])        {            P[pnum + +] = i;               Facnum[i] = 2;              D[i] = 1;              }        for (int j = 0; J < pnum && I * p[j] < MAX; j + +)        {            noprime[i * p[j]] = true;            if (i% p[j] = = 0)            {                facnum[i * p[j]] = facnum[i]/(D[i] + 1) * (D[i] + 2);                 D[i * P[j]] = d[i] + 1;                 break;            }            Facnum[i * P[j]] = facnum[i] * 2;            D[i * P[j]] = 1;}}    



Four-in-oneness

void Get_all () {    int pnum = 0;    PHI[1] = 1;    MOB[1] = 1;    FACNUM[1] = 1;    for (int i = 2; i < MAX; i++)    {        if (!noprime[i])        {            phi[i] = i-1;            Mob[i] =-1;            P[pnum + +] = i;               Facnum[i] = 2;              D[i] = 1;              }        for (int j = 0; J < pnum && I * p[j] < MAX; j + +)        {            noprime[i * p[j]] = true;            if (i% p[j] = = 0)            {                phi[i * p[j]] = phi[i] * P[j];                Mob[i * P[j]] = 0;                Facnum[i * P[j]] = facnum[i]/(D[i] + 1) * (D[i] + 2);                 D[i * P[j]] = d[i] + 1;                 break;            }            Phi[i * P[j]] = phi[i] * (P[j]-1);            Mob[i * P[j]] =-mob[i];            Facnum[i * P[j]] = facnum[i] * 2;            D[i * P[j]] = 1;}}    

Finally spit the trough, for the prime sieve in the judgment function, it is best to use noprime, because the global default value is False, sometimes Max is 1e7 and so on, memset to true also cost a lot

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Number theory Linear sieve summary (prime sieve, Euler function sieve, Möbius function sieve, first n number of approximately several sieve)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.