√ N evaluate single-valued Euler's function, √ n Euler's Function

Source: Internet
Author: User

√ N evaluate single-valued Euler's function, √ n Euler's Function
Basic theorem:



First, let's take a look at the core code:

Core code



Principle Analysis:

At the beginning, I couldn't understand this code. I had the following problems:

1. Didn't n * xxx be written in the theorem at the beginning? Why is there no * n in the code?

2. Isn't ans * (prime [I]-1? Why does the second while loop become * prime [I?

3. Isn't pi required in the theorem? Why is there no/pi ?????????????


Formula simplicity

First, let's analyze the principle of the entire program. If we understand the principle of the program, these three problems will be solved naturally.

The principle of this program is based on the unique decomposition theorem:


Then we can take n apart, bring it back to the Euler's function formula, and then share the following points:

LaTex code:

1 ans=p_1^a^1*p_2^a^2*.......*p_i^a^i*\frac{p_1-1}{p_1}*\frac{p_2-1}{p_2}*....*\frac{p_i-1}{p_i}2    \newline 3    =p_1^a^1*\frac{p_1-1}{p_1}*.......*p_2^a^2*\frac{p_2-1}{p_2}*....p_i^a^i*\frac{p_i-1}{p_i}4    \newline 5    =p_1^a^{1-1}*({p_1-1})*.......*p_2^a^{2-1}*({p_2-1})*....p_i^a^{i-1}*({p_i-1})
Answer questions

First of all, there is another tips for code implementation:
We put x/prime [I] before while, which is equivalent to making ans less * a prime [I], so we can solve the problem of exponential ai-1.


Now let's take a look at the first three questions.



The answer is as follows,

But please think carefully and then read it again,












The answer is as follows:














1. Didn't n * xxx be written in the theorem at the beginning? Why is there no * n in the code?

Because n is uniquely decomposed, the content in the while loop is used to * n

2. Isn't ans * (prime [I]-1? Why does the second while loop become * prime [I?

* Prime is used to final the answer * n

3. Isn't pi required in the theorem? Why is there no/pi ?????????????

Simplified. If you do not understand it, you can refer to the simplification process above.



Complete code
 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 using namespace std; 6 const int MAXN=1000001; 7 int prime[MAXN]; 8 int mu[MAXN]= {0,1}; 9 int n;10 int tot=0;11 int vis[MAXN]= {1,1};12 void read(int &n) {13     char c='+';14     int x=0;15     bool flag=0;16     while(c<'0'||c>'9') {17         c=getchar();18         if(c=='-')flag=1;19     }20     while(c>='0'&&c<='9') {21         x=x*10+c-48;22         c=getchar();23     }24     flag==1?n=-x:n=x;25 }26 void ou() {27     for(int i=2; i<=n; i++) {28         if(!vis[i])29             prime[++tot]=i,mu[i]=-1;30         for(int j=1; j<=tot&&j*prime[i]<=n; j++) {31             vis[i*prime[j]]=1;32             if((i%prime[j])==0) {33                 mu[i*prime[j]]=0;34                 break;35             }36             mu[i*prime[j]]=-mu[i];37         }38     }39 }40 int getphi(int x) {41     int ans=1;42     for(int i=1; i<=tot&&prime[i]*prime[i]<=x; i++) 43     {44         if(x%prime[i]==0) 45         {46             ans*=(prime[i]-1);47             x=x/prime[i];48             while(x%prime[i]==0) 49             {50             ans*=prime[i];51             x/=prime[i];52             }53         }54         55     }56     if(x>1)57         ans*=x-1;58     return ans;59 }60 int main() {61     n=1001;62     ou();63     int c;64     printf("please input the num\n");65     while(cin>>c)66         printf("the num`s phi is %d\n",getphi(c));67     return 0;68 69 }


The method for finding the Mobius function in a linear manner is also introduced ,,

Too lazy to delete ,,,


End with a few wordings

Here is the single-value Euler's function,

In fact, there is still a very mysterious way to understand this code,

However, this method is easy to understand,

These two methods are essentially the same.

I will not repeat it here


Finally, we will introduce only the method for finding the single-value Euler's function,

Actually, Euler's function also has a linear screening method (because Euler's function is a product function)

Please try again later.


In addition, because I am the first time in contact with the Euler function, this article will certainly have a bunch of bugs. If you find a bug, you can leave a message in the comment area or contact me by other means,


Thank you!

Related Article

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.