banner
genkaim

genkaim

Darkmode,yes \q(≧▽≦q) 主题来自@birdgg

Prime Number Sieve | Euler's Function | Euler's Sieve Method and Eratosthenes' Sieve Method Detailed Explanation

cover

Definition#

The Euler's Totient Function is a function that counts the number of positive integers less than or equal to nn that are coprime to nn. It is denoted as ϕ(n)\phi(n).

example

ϕ(2)=1\phi(2) = 1, (1)
ϕ(3)=2\phi(3) = 2, (1, 2)
ϕ(4)=2\phi(4) = 2, (1, 3)
ϕ(5)=4\phi(5) = 4, (1, 2, 3, 4)
ϕ(6)=2\phi(6) = 2, (1, 5)

Formula#

Conclusion#

Assuming the prime factorization of nn is p1a×p2b×...×pmkp_1^a \times p_2^b \times ... \times p_m^k (pip_i is a prime factor), the Euler's Totient Function can be calculated using the formula:
ϕ(n)=n×(11p1)×(11p2)×...×(11pm)\phi(n) = n \times (1- \cfrac{1}{p_1} ) \times (1- \cfrac{1}{p_2} ) \times ... \times (1-\cfrac{1}{p_m})

Derivation#

First, if a number xx is coprime to nn, it means that they do not have any common prime factors, i.e., xx cannot have prime factors such as p1p_1, p2p_2, ..., pmp_m.
So, the number of integers in the range 1...n1...n that have p1p_1 as a prime factor is npi\cfrac{n}{p_i}.
Similarly, for each prime factor pip_i, the number of integers in the range 1...n1...n that have pip_i as a prime factor is npi\cfrac{n}{p_i}.
Obviously, the number of integers without the prime factor pip_i is n(npi)n - \sum(\cfrac{n}{p_i}).

By adding the numbers that were subtracted repeatedly, we can use the principle of inclusion-exclusion to obtain:

=n(1- \cfrac{1}{p_1}) \times (1-\cfrac{1}{p_2})...(1-\cfrac{1}{p_m})$$

Naive Algorithm#

1#

According to the definition, directly enumerate the number of integers in the range 1...n1...n that are coprime to nn and have the greatest common divisor of 1.

  for(int i=1; i<=n; i++)
    ans += gcd(n, i)==1;

2#

According to the formula

ϕ(n)=n×(11p1)×(11p2)×...×(11pm)\phi(n) = n \times (1- \cfrac{1}{p_1} ) \times (1- \cfrac{1}{p_2} ) \times ... \times (1-\cfrac{1}{p_m})

Loop through the prime factors and calculate ans=ans*(1-1/pi), i.e., ans-= ans/pi

  ans = n;
  for(int i=2; i*i<=n; i++) {
    if(n%i==0) {
      ans -= ans/i;
      while(n%i==0) n/=i;
    }
  }
  if(n>1) ans -= ans/n;

Sieve Method#

Eratosthenes Sieve Method#

According to the definition of the Euler's Totient Function, ϕ(x)=x×(11pi)\phi(x)=x \times (1- \cfrac{1}{p_i})

The principle of the Eratosthenes Sieve Method is to sieve all multiples of the prime number pip_i, indicating that j×pij \times p_i has a prime factor pip_i, so it can be calculated using the formula mentioned above.

  • First, phi[x] = x
  • For each multiple j of the prime number pip_i, phi[j] *= (1-1/pi), which means phi[j] -= phi[j]/pi
    • If for some i, phi[i] != i, it means it has been sieved before and is a composite number
void sieve(int mx) {
  for(int i=1; i<=mx; i++)
    phi[i] = i;
  for(int i=2; i<=mx; i++) {
    if(phi[i] != i) continue;
    for(int j=i; j<=mx; j+=i)
      phi[j] -= phi[j]/i;
  }
}

Euler's Sieve Method#

For a prime number pip_i, there are two cases to consider:

  1. If it is a prime factor of x, then for x×pix \times p_i, pi has already been calculated in the Euler's Totient Function (11pi)(1-\cfrac{1}{p_i}), so we have: ϕ(x×pi)=ϕ(x)×pi\phi(x \times p_i) = \phi(x) \times p_i
  2. If it is not a prime factor of x, then for x×pix \times pi, there is an additional prime factor, so we have: ϕ(x×pi)=ϕ(x)×pi×(11pi)=ϕ(x)×(pi1)\phi(x \times p_i) = \phi(x) \times p_i \times (1- \cfrac{1}{p_i}) = \phi(x) \times (p_i - 1)

Combining with the Euler's Sieve Method, we can obtain the code to calculate phi[x].

void sieve(int mx) {
  phi[1] = 1;
  for(int i=2; i<=mx; i++) {
    if(!notPrime[i]) {
      p[++last] = i;
      phi[i] = i-1;
    }
    for(int j=1; j<=last; j++) {
      int t = i * p[j];
      if(t>=mx) break;
      notPrime[t] = true;
      if(i % p[j]==0) {
        phi[t] = phi[i] * p[j];
        break;
      } else
       phi[t]= phi[i] * (p[j]-1);
    }
  }
}
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.