Introduction
In number theory, for a positive integer n, Euler’s totient function is the number of numbers less than n that are coprime to n. This function is named after its first researcher, Euler (Ruler’so totient function); it is also called Euler’s totient function, the φ function, Euler’s quotient, etc. For example, φ(8)=4, because 1, 3, 5, and 7 are all coprime to 8. The facts in ring theory derived from Euler’s totient function and Lagrange’s theorem constitute the proof of Euler’s theorem.
Properties
$\varphi \left( x\right) =x\left( 1-\dfrac{1}{p_{1}}\right) \left( 1-\dfrac{1}{p_{2}}\right) \ldots \left( 1-\dfrac{1}{p_{n}}\right)$
If n is the k-th power of a prime p
$\varphi \left( n\right) =p^{k}-p^{k-1}$
(because except for multiples of p, all other numbers are coprime to n)
If n is prime, then
$\varphi \left( n\right) = n-1$
These theorems form the basis of computing Euler’s totient function with a sieve.
Algorithm Implementation
Direct Calculation
| |
Sieve Method
| |
References:
Euler’s Totient Function - Baidu Baike
Euler’s Totient Function linear sieve
Euler’s Totient Function_Lur ’s acm blog