1. 程式人生 > >【數論】尤拉函式

【數論】尤拉函式

尤拉函式

 

在數論,對正整數n,尤拉函式是小於n的正整數中與n互質的數的數目(φ(1)=1)。此函式以其首名研究者尤拉命名(Euler'so totient function),它又稱為Euler's totient function、φ函式、尤拉商數等。 例如φ(8)=4,因為1,3,5,7均和8互質。 從尤拉函式引伸出來在環論方面的事實和拉格朗日定理構成了尤拉定理的證明。

通式   

 

尤拉函式有兩個性質

也是這份程式碼使用的兩個性質

若n是p的k次方 那麼phi[n] = n - k;

若m, n互質 那麼

若n = q1 ^ a1 + q2 ^ a2 + q3 ^ a3  ……

則 phi[n] = n * (1 - 1 / q1 ) * (1 - 1 / q2 )  ……

板子:

const int N = 10000005;
    int phi[N];
    inline void phi_table(int x){
	int i, j;
	phi[1] = 1;
	for(i = 2; i <= x; i++)phi[i] = 0;
	for(i = 2; i <= x; i++){
		if(phi[i] == 0)
		    for(j = i; j <= x; j += i){
		    	if(phi[j] == 0) phi[j] = j;
		    	phi[j] = phi[j] / i * (i - 1);
		    }
	}
    }