歐拉函數的計算

NOIp要來了,最後Orz一把LRJ.
歐拉函數
歐拉函數,別的計算方法不會不多證明.直接上代碼

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>

using namespace std ;

int euler_phi ( int x ) {
    int i, j, k, ans = x, lim = (int)sqrt(x+0.5) ;
    for ( i = 2 ; i <= lim ; i ++ ) 
        if ( x % i == 0 ) {
            ans = ans / i * ( i-1 ) ;
            while ( x%i==0 ) x /= i ;
        }
    if ( x > 1 ) ans = ans / x * (x-1) ; 
    //如果剩下的x也是原來x的因數
    return ans ;
}

int main() {
    int i, j, k, n, m ;
    while ( scanf ( "%d", &n ) != EOF ) 
        printf ( "Euler_phi(%d) = %d\n", n, euler_phi(n) ) ; 
    return 0 ; 
}
發佈了55 篇原創文章 · 獲贊 39 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章