所有Java中小于或等于n的数字的Euler Totient函数
下面是一个程序,当给定n时,对于所有小于或等于n的数字,获取Euler的Totient函数的结果。
程序
import java.util.Scanner;
public class EulerTotient {
public static int gcd(int a,int b){
int i, hcf = 0;
for(i = 1; i <= a || i <= b; i++) {
if( a%i == 0 && b%i == 0 )
hcf = i;
}
return hcf;
}
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("输入n值:");
int n = sc.nextInt();
for (int i = 1; i <= n; i++){
int x = 1;
for (int j = 2; j < i; j++){
if (gcd(j, n) == 1){
x++;
}
}
System.out.println(x);
}
}
}输出结果
Enter the n value 10 1 1 1 2 2 2 2 3 3 4
热门推荐
6 保研的祝福语简短
10 年轻20岁祝福语简短
11 朋友结婚祝福语信息简短
12 女孩婚礼贺卡祝福语简短
13 30段点歌简短祝福语
14 虎年春节祝福语图文简短
15 写给后妈祝福语大全简短
16 简短回复生日祝福语
17 校长送毕业祝福语简短
18 毕业立体贺卡祝福语简短