欧拉函数求小于或等于给定数 n 的所有数的 java


下面是一个程序,该程序可以在给定 n 后得到小于或等于 n 的所有数的欧拉函数结果。

程序

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("Enter the n value :");
      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

更新时间:2020-6-25

896 浏览量

开启你的 职业生涯

完成课程,获得认证

开始
广告