import java.util.Scanner;
public class Main {
static int[] phi;
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
phi = new int[n+1];
getPhi(n,phi);
System.out.println(phi[n]);
}
public static void getPhi(int n,int[] phi){
for(int i = 2;i<=n;i++){
phi[i] = 0;
}
//规定phi[1] = 1
phi[1] = 1;
for(int i = 2;i<=n;i++){
if(phi[i] == 0){
for(int j = i;j<=n;j += i){
if(phi[j] == 0){
phi[j] = j;
}
phi[j] = phi[j]/i*(i-1);
}
}
}
}
}
欧拉phi函数求 从1到n中与n互素的数的个数 Java
最新推荐文章于 2025-07-24 10:00:06 发布