Euler function: Find the number of numbers less than or equal to n and coprime with n

Find the number of numbers less than or equal to n and relatively prime to n

coprime brute force method

  1. Coprime: Two numbers are coprime, which means that the greatest common divisor of the two numbers is 1
  2. The method of calculating the greatest common divisor: rolling and dividing, the least common multiple: dividing the larger value by the greatest common divisor and multiplying the smaller value
  3. Rolling and dividing method:
    1. The larger number a takes the modulus of the smaller number b, and the modulus value c is obtained
    2. If the modulus value is equal to 0, then the greatest common divisor is the modulo value, otherwise continue to the next step
    3. Take the modulus of a and c again, and return to the second step
    //求最大公约数gcd以及最大公倍数lcm
     // 36 24 36/24
     // 24 12 24/12
     // 0 结束最大公约数为12
     // 求最小公倍数
     // lcm(a, b) = (a * b)/gcd(a, b)
     public static int gcd(int a, int b){
          
          
         //a>=b
         //辗转相除法
         if (b==0){
          
          
             return a;
         }
         return gcd(b,a%b);
     }
    
  4. Exhaustive to n, one by one to determine whether the greatest common divisor of the number and n is 1, that is, whether it is coprime

Conclusion: It can be achieved, but the time complexity is too high

Take the Euler function to find

In number theory, for a positive integer n, Euler's function is the number of numbers that are relatively prime to n among positive integers less than or equal to n.

n is a positive integer n, p1, p2...pn are prime factors of positive integer n

Prime factor of n: a number that is both a factor of n and a prime number

Calculation method:
ϕ ( n ) = n × ( p 1 − 1 p 1 ) × ( p 2 − 1 p 2 ) ⋯ × ( pn − 1 pn ) \phi (n) = n \times (\frac{p_1- 1}{p_1})\times (\frac{p_2-1}{p_2})\cdots\times (\frac{p_n-1}{p_n})ϕ ( n )=n×(p1p11)×(p2p21)×(pnpn1)
例:
ϕ ( 10 ) = 10 × 1 2 × 4 5 = 4 \phi (10) = 10 \times \frac{1}{2}\times \frac{4}{5} = 4 ϕ ( 10 )=10×21×54=4

  1. How to find a prime number: the only factors are 1 and itself
    1. Judgment of a single prime number n

      Sequentially judge whether the value of the number from 2 to $\sqrt{n}$ modulo n is equal to zero, if there is any one, it is not a prime number

      When p is greater than n \sqrt{n}n , the representative number p must be able to get a less than! n \sqrt{n}n and a number greater than n \sqrt{n}n Pairwise factors of , not prime

    2. Judgment of prime numbers from 2 to n

      Non-exhaustive, exhaustive time complexity is O(n), using the prime number sieve method is O( log ⁡ n \log_{}{n}logn)

      To ensure efficiency, prime numbers are false and composite numbers are true

      1. The numbers marked 2 to n are all prime numbers, which is false, and the default value of the Boolean array is false, so there is no need to mark them one by one

      2. Starting from 2 to mark the number, find the first number p that is false

      3. The multiple of the mark number p is a composite number, which is true, and the multiple mark is from p × pp \times pp×p starts, until the number p is equal to $\sqrt{n}$, the end marker

        reason:

        The factor of the multiple of p must have p, which does not meet the prime number condition, every time from p × pp \times pp×The p start tag is due top − p pppThe part of p has already been marked and will not be marked again.

  1. Make the next number p a number that is not marked as a composite number, that is, a number whose value is still false, repeat the third step

  2. Mark as false, that is, all output of prime numbers

  1. When using the prime number sieve method to find prime numbers, the operation of the multiple mark can be modified to multiply by (1-1/p), so that each number can be multiplied by its prime factor

img

  1. Store them in the array in turn, and finally output the results in sequence.
public static int f1(int n){
    
    
        int res = n;
        for (int i = 2;i*i<=n;i++){
    
    
            if (n % i==0){
    
    
                res = res / i*(i-1);//res/i
                while (n % i == 0){
    
    
                    n/=i;
                }
            }
        }
        if (n>1){
    
    
            res = res/n*(n-1);
        }
        return res;
    }
    //区间内欧拉函数取值
    public static int[] f2(int n){
    
    
        int[] count = new int[n+1];
        for (int i = 1;i <= n;i++){
    
    
            count[i]=i;
        }
        for (int i =2 ;i <= n;i++){
    
    
            if (count[i] == i){
    
    
                for (int j = i;j <= n;j+=i){
    
    
                    count[j] = count[j]/i*(i-1);
                }
            }
        }
        return count;
    }

Knowledge points:

  1. greatest common divisor, least common multiple

  2. Single prime number judgment

  3. Prime number sieve method: Elsieve sieve method

  4. Euler function

Ask for likes and forward

Guess you like

Origin blog.csdn.net/yumuing/article/details/122954314