Algorithm-Euler function __ Algorithm-Euler function

Source: Internet
Author: User

Introduction to Euler functions --Excerpts from Baidu Encyclopedia

Note The following three special properties

Programmatic Implementation
By using the relation of Euler function and its different mass factor, the Euler function value of all numbers in a certain range is calculated by Sieve method.

Solve the Euler function directly  
#include <cstdio>
int Euler (int n) {//return Euler (n)   
    int res=n,a=n;  
    for (int i=2;i*i<=a;i++) {////Small to large try N's mass factor 
        if (a%i==0) {//If I is the mass factor of n 
            res=res/i* (i-1); a 1/i comes out, The first Division is to prevent the overflow of intermediate data while   
            (a%i==0) a/=i;//Euler functions are only counted as a mass factor 
        }  
    if (a>1) res=res/a* (A-1); If the last remaining factor return 
    res;  
}
int main () {
    int x;
    scanf ("%d", &x);
    printf ("%d", Euler (x));
    return 0;
}
//screening method play Euler function table #include <cstdio> #define MAX 1000001 int Euler[max];
    void Init () {euler[1]=1;
    for (int i=2;i<max;i++) euler[i]=i; for (int i=2;i<max;i++) if (euler[i]==i)//If I is prime number for (int j=i;j<max;j+=i) euler[j
]=euler[j]/i* (i-1), or to mention a 1/i, First division is to prevent the overflow of intermediate data return;
    int main () {Init ();
    for (int i=1;i<=100;i++) printf ("%d\n", Euler[i]);
return 0; }

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.