欧拉函数

参考于:http://blog.csdn.net/sentimental_dog/article/details/52002608

欧拉函数

    对φ(N)的值,我们可以通俗地理解为小于N且与N互质的数的个数(包含1)

1.对于素数p, φ(p)=p-1,对于对两个素数p,q φ(pq)=pq-1

欧拉函数是积性函数,但不是完全积性函数.

2.对于一个正整数N的素数幂分解N=P1^q1*P2^q2*...*Pn^qn.

   φ(N)=N*(1-1/P1)*(1-1/P2)*...*(1-1/Pn).

3.除了N=2,φ(N)都是偶数.

公式求欧拉函数的值 O(sqrt(n)):

 1 int euler(int x)
 2 {
 3     int res = x;
 4     for (int i=2;i*i<=x;i++)
 5     {
 6         if (x%i==0)
 7         {
 8             res = res/i*(i-1);
 9             while (x%i==0) x/=i;
10         }
11     }
12     if (x>1) res = res/x*(x-1);
13     return res;
14 }
View Code

 

如果要求 1 ---- n  之间的所有欧拉函数的值,就用线性筛法

 

1 void erler()
2 {
3     for (int i=1;i<MX;i++)
4         erlertb[i]=i;
5     for (int i=2;i<=MX;i++)
6         if (erlertb[i]==i)
7             for (int j=i;j<MX;j+=i)
8                 erlertb[j] = erlertb[j]/i*(i-1);
9 }
View Code

 

 

 

 

posted @ 2017-08-01 20:49  happy_codes  阅读(177)  评论(0编辑  收藏  举报