zoukankan      html  css  js  c++  java
  • 欧拉函数

    定义

    对于一个正整数n,小于n且和n互质的正整数(包括1)的个数,记作φ(n) 

    φ(x) = x (1 - 1/p(1) )(1 - 1/p(2) )(1 - 1/p(3) )(1 - 1/p(4) )…..(1 - 1/p(n) )

    其中p(1),p(2)…p(n)为x的所有质因数、x是正整数、 φ(1)=1(唯一和1互质的数,且小于等于1)。

    注意:每种质因数只有一个

         例如:

             φ(10)=10×(1-1/2)×(1-1/5)=4;

             1 3 7 9

             φ(30)=30×(1-1/2)×(1-1/3)×(1-1/5)=8;

             φ(49)=49×(1-1/7)=42;

    应用

    代码

    求与26互素的个数

    #include <stdio.h>
    #include <stdlib.h>
     
    int eular(int n)
    {
        int ans = n;
        for(int i=2; i*i <= n; i++)
        {
            if(n%i == 0)//i是质因数
            {
                ans = ans/i*(i-1);
                while(n%i == 0)//确保不会出现合数因子
                    n/=i;
            }
        }
        if(n > 1) 
            ans = ans/n*(n-1);
        //因为i是从2开始,所以上面的循环无法判断n是素数的情况,因此在这加个判断
        return ans;
    }
    int main() {
            int n = 27;
            printf("%d", eular(n));
            return 0 ;
    }
    
  • 相关阅读:
    算法笔记--二分图判定
    算法笔记--最小表示法
    Codeforces 525A
    Codeforces 140D
    Codeforces C
    Codeforces 665C
    Codeforces 604B
    Codeforces 285C
    The Fewest Coins POJ
    Triangular Pastures POJ
  • 原文地址:https://www.cnblogs.com/pam-sh/p/13762123.html
Copyright © 2011-2022 走看看