尤拉函式求小於或等於給定數 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
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP