尤拉函式求小於或等於給定數 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 瀏覽量

開啟你的 職業生涯

完成課程,獲得認證

開始
廣告