Euler function, denoted by φ (n)
The Euler function is the number of numbers in the number of n coprime less than n
Spicy, how to beg? ~ (~o ̄▽ ̄) ~o
Can be found in 1 to n-1 with N not coprime number, and then cut them off
Like φ (12)
The decomposition of the 12 qualitative factor, 12=2*2*3, is actually obtained 2 and 32 of the quality factor
And then delete the multiples of 2 and the multiples of 3.
Multiples of 2:2,4,6,8,10,12
Multiples of 3:3,6,9,12
I was going to use 12-12/2 directly-12/3.
But 6 and 12 have been repeatedly reduced.
So we're going to add a number that is a multiple of 2 and a multiple of 3 (>﹏<)
So write 12-12/2-12/3 + 12/(2*3)
What is it called, this is called the repulsion, the law of tolerance.
such as φ (30), 30 = 2*3*5
So φ (30) = 30-30/2-30/3-30/5 + 30/(2*3) + 30/(2*5) + 30/(3*5)-30/(2*3*5)
But it's a hassle to write ( ̄. ̄).
There's an easy way
φ (12) = 12* (1-1/2) * (1-1/3) = 12* (1-1/2-1/3 + 1/6)
φ (30) = 30* (1-1/2) * (1-1/3) * (1-1/5) = 30* (1-1/2-1/3-1/5 + 1/6 + 1/10 + 1/15-1/30)
You see (?∀?), and when you open it, you find that it helps you to do it automatically.
So φ (30) is calculated by first looking at the 30 quality factor.
2,3,5, respectively.
Then use 30* 1/2 * 2/3 * 4/5 to get it done.
The code is as follows:
1 //Euler functions2 intPhiintx) {3 intAns =x;4 for(inti =2; I*i <= x; i++)5 if(x% i = =0){6Ans = ans/i * (i-1);7 while(x% i = =0) x/=i;8 }9 if(X >1) ans = ans/x * (X-1);Ten returnans; One}
(Phi is the pronunciation of φ)
Wit's code, WIT's Me (?? ' Ω´?
The complexity of this is O (√n), if you want to find the number of n Euler function, the complexity is O (n√n), which is too slow
There are faster ways to wire sieve Euler functions
Need to use the following properties
P is prime
1. Phi (p) =p-1 because the prime number p except 1 is only p, so the integer 1 to p is only p and p not coprime
2. If I mod p = 0, then phi (i * p) =phi (i) * p (I will not prove)
3. If I mod p≠0, then phi (i * p) =phi (i) * (p-1) (I will not prove)
(so I said I'd prove it was a lie. ╮ ( ̄▽ ̄) ╭)
The code is as follows:
1#include <cstdio>2 using namespacestd;3 Const intM = 1e6+Ten ;4 intPhi[m], prime[m];5 intTot//tot count, indicating how many prime numbers are in prime[m]6 intEuler () {7 for(inti =2; i < M; i + +) {8 if(!Phi[i]) {9Phi[i] = i1 ;Tenprime[++tot] =i; One } A for(intj =1; J <= tot && 1ll*i*prime[j] < M; J + +) { - if(i% prime[j]) phi[i * Prime[j]] = phi[i] * (prime[j]-1) ; - Else { thePhi[i * Prime[j]] = phi[i] *Prime[j]; - Break ; - } - } + } - } + A intMain () { at Euler (); -}
(Euler is Euler)
Wit's code, WIT's Me (?? ' Ω´?
Number Theory Tour 4---the proof of Euler function and the implementation of code (I'll prove it's all a Lie ╮ ( ̄▽ ̄) ╭)