Analysis of Euler function algorithm —————— mathematics ——————

Source: Internet
Author: User

In number theory, for a positive integer n, the Euler function is less than or equal to N and the number of the number n coprime.

N's Euler function value is phi (n)

For example Phi (8) =4 (4 and 8 coprime respectively 1 3 5 7)

Formula: in which P1, p2......pn is all decomposition of x, X is an integer that is not 0. φ (1) =1 (the number of unique and 1 coprime (less than or equal to 1) is 1 itself). (Note: Only one of each quality is due.) such as 12=2*2*3 φ (12) =12* (1-1/2) * (1-1/3) =4

So Phi (x) template is:

int phi (int x) {//Note: Ans is the value of Phi (x)
    int ans=x;//shilling phi (x) =x, the previous part of the formula for
    (int i=2;i*i<=x;i++) {//Enumerate each number (note, Phi requires a prime number, and here is an enumeration of all numbers. As for why this enumeration, see the code below the explanation) if
        (x%i==0) {//If this number is a factor
            ans-=ans/i;//sleeve formula x* (1-1/PI) =x-x/pi namely here ans-=ans/i;
            while (x%i==0) x/=i;//sieve the number of all I-factor
        }
    if (x>1)//(x)////Screen after the screen has the remainder, which indicates that this number is also a mass factor
        ans-=ans/x;//the empathy set formula Return
    ans;//Answer
}
Explain why you want to enumerate each number here:

It's very simple. "Small to large enumeration per number" and "small to large enumeration per prime" are the same in this template

Because all composite numbers can be decomposed into the form of prime number multiplication.

Note the while (x%i==0) x/=i;

X will screen the mass factor every time,

Like in Phi (8)

4 This composite factor has been screened for 2 of this mass factor.

Because of the 4=2*2, all factor 2 has been sieved to 2 o'clock.

At this point, the value of x in Phi (x) has changed and is no longer 8.

When you sift to 4, x%4 won't ==0.



So what do we do if we need to play Phi (MAXN) table? You don't have to recycle the top template from 1 to MAXN. We have a more witty approach.

Template:

int P[MAXN];
void Phi () {for
    (int i=1;i<maxn;i++) p[i]=i;
    for (int i=2;i<maxn;i++) {
        if (p[i]==i) {for
            (int j=i;j<maxn;j+=i)
                p[j]-=p[j]/i
        }
}}
If you're going to sift the primes, congratulations, this is a prime sieve.

It's just that every prime we sift out in the prime is used to save it.

And here we sift out each prime

As a vegetarian factor

"Proactively" to find the number that contains this factor.

Then set the formula.



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.