Learning diary number three - Euler Euler function and linear sieve

Well, since a few days saw number theory and found each time to see an afternoon passed, today extended Euclidean inverse yuan to get after catching a moment to Euler Euler function and linear sieve, said about today's income it:

 

What Euler functions are?

In number theory, for positive integer n, the Euler function is a positive integer less than or equal to n with n prime number of number. (What is coprime? Coprime generally refers to a greatest common divisor of two numbers is the prime)

Euler functions are generally used "φ (n) = .." expressed

 

The basic nature of the use to which the Euler function?

One:

When [Phi] (n) is a prime number n, φ (n) = n - 1, because when n is a prime number, and only that itself does not prime itself.

 

two:

If m mod p = 0, then phi (m * p) = p * phi (m).

 prove:

Suppose b = gcd (n, m) n, m is not coprime ∴ n = k1 · b, m = k2 · b

∴ n + m = (k1 + k2) b ∴n + m and m is not coprime

Integer [1, m] in the m is not coprime with n co m - φ (m) number.

m and n + m is not coprime ∴ [1 + m, m + m], ie (m, an integer of 2M] m is not coprime with also m-φ (m) a

∴ [1, m · p] m is not coprime with the integer m · pp · φ (m) a

∵ [1, m · p] with not coprime m · p is an integer skilled m · p - p · φ (m) = m · p - φ (m · p)

∴φ(m·p) = p · φ(m)

 

 III: ! If m mod n = 0, then φ (m * n) = φ (m) * (n-1) = φ (m) * φ (n); This can be seen

φ (m * n) is a multiplicative function.

 

four:

 

 

How Euler function calculation?

To learn more about the Gangster https://blog.csdn.net/liuzibujian/article/details/81086324 liuzibujian can refer to the blog (written in a very clear). Here it simple for everyone to look at:

 

 

Having Euler function, we can go further understand the Euler linear screen!

Euler linear sieve effect is all screening a range of composite number (remove), and obtains all known prime number within this range for each Euler function.

 

As many chiefs not talking about very detailed ah, and I'm not a big brother, so I try to use various summaries of various debugging easiest argument to clear rationale Euler screen in the end is what.

 

I will say that under the principle of it, selected portion of a number of co-roughly with the prime my previous blog said screening method is very similar, but this is a multiple screening, faster than before the screening, with a minimum quality factor to filter out all the numbers together and mark.

Code is as follows (within 100 Euler mesh):

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<malloc.h>
 5 #include<memory.h>
 6 #include<conio.h>
 7 using namespace std;
 8 
 9 int phi[101], prime[101];
10 bool p[101];
11 
12 int main() {
13     memset(p, true, sizeof(p));
14     int cnt = 0;
15     phi[1] = 1;
16     for (int i = 2; i <= 100; ++i) {
17         if (p[i]) {
18             prime[++cnt] = i;//2,3,5,7
19         }
20         for (int j = 1; j <= cnt && prime[j] * i <= 100; ++j)
21         {
22             p[prime[j] * i] = false;
23 
24             if (i%prime[j] == 0) break;
25         }
26     }
27 
28     for (int i = 2; i <= 100; i++)
29         if (p[i])cout << i << endl;
30     _getch();
31     return 0;
32 }

 

Well little by little, first three array phi [], prime [], p [], their effects are kept three Euler function, storage prime number screening.

The first step: we have an array of all the p type bool initialized to true

Step two: Go to first for loop, first determine if i is prime, if it is a prime number is stored in the prime [] (first look people may ask, why did go judgment p [i ], p [i] do not entirely true, it is not all numbers are deposited into it? but please do not worry, at the beginning I said, this is the use of minimum quality factor to filter out all composite number, marked as false, 2 is a prime number, right? Well, from 2 to start screening is not entering the second for loop, p [prime [j] * i] put 4 to filter out, and once on, the back will be 3 to 6 screened, it is a prime number is still true, is the number of combined is not turned into a false?)

The third step: to enter the second cycle, the screening phase, p [prime [j] * i] In the second step has been explained, then if (i% prime [j] == 0) break; what does it mean ? Or that the focus is to ask each composite number to be determined by its smallest prime factor weed out. It assumed that the current I is a multiple prime [j], then i = n × prime [j] (n∈N *) necessarily true. If we do not do not out of the loop when the prime [j + 1] when, I will go sieve I × prime [j + 1], since i = n × prime [j], so I × prime [j + 1] = n × prime [j] × prime [j + 1], which is not what we want to minimize the quality factor, it will not require a lot of extra steps increase the complexity.

 

 

OK, we have screened will, the next is a function of the Euler -

code show as below:

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<malloc.h>
 5 #include<memory.h>
 6 #include<conio.h>
 7 using namespace std;
 8 
 9 int phi[101], prime[101];
10 bool p[101];
11 
12 int main() {
13     memset(p, true, sizeof(p));
14     int cnt = 0;
15     phi[1] = 1;
16     for (int i = 2; i <= 100; ++i) {
17         if (p[i]) {
18             prime[++cnt] = i;
19             phi[i] = i - 1;
20         }
21         for (int j = 1; j <= cnt && prime[j] * i <= 100; ++j)
22         {
23             p[prime[j] * i] = false;
24 
25             if (i%prime[j] == 0)
26             {
27                 phi[i*prime[j]] = prime[j] * phi[i];
28                 break;
29             }
30             else {
31                 phi[i*prime[j]] = phi[i] * phi[prime[j]];
32             }
33         }
34     }
35 
36     //for (int i = 2; i <= 100; i++)
37         //if (p[i])cout << i << endl;
38     for (int j = 1; j <= 100; j++)
39         cout << phi[j] << endl;
40     _getch();
41     return 0;
42 }

 

Is in fact the original code more than a few lines of change, the first is to determine the number of statements in the stroma much phi [i] = i - 1 ; I said before the if n is a prime number, then the corresponding φ (n) = n - 1 the second process is the smallest prime factors of screening, is apparent from the nature of the second and third property before, prime [j] is a prime number, i is an arbitrary number, and i% prime [j] == 0, then, then phi (m * p) = p * phi (m). (p is a prime number), if i% prime [j]! = 0 , then, phi [I * Prime [J]] = phi [I] * phi [Prime [J]] ;, thus ensuring phi each a value is updated. For the special case Phi [1], we have to make direct phi [1] = 1, because the number 1 is neither bonded nor is a prime number, but according to the international requirements, is a function of the Euler 1.

 

In summary, we have completed all of the steps, and this is the role of the Euler linear screen, we should be able to understand, after all, I was too dishes.

---------------------------------------------------------------------------------------------------

 

Guess you like

Origin www.cnblogs.com/xiangqi/p/11128632.html