我想找到 GCD=1 到某个数字的对,比如 10000。我正在使用 2 个嵌套循环并调用一个带有长参数的方法。但是代码运行缓慢,需要任何有效的方法。谢谢
class FastGCD {
public static long GCD(long a, long b) {
return (b == 0 ? a : GCD(b, a % b));
}
public static void main(String ah[]) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int cases = 0;
long number = 0, output = 0;
try {
cases = Integer.parseInt(br.readLine());
} catch (NumberFormatException e) {
System.exit(0);
} catch (IOException e) {
System.exit(0);
}
for (int i = 1; i <= cases; i++) {
try {
number = Long.parseLong(br.readLine());
} catch (NumberFormatException e) {
e.printStackTrace();
System.exit(0);
} catch (IOException e) {
e.printStackTrace();
System.exit(0);
}
for (int j = 0; j < number; j++) {
for (int k = 0; k < number; k++) {
if (FastGCD.GCD(j, k) == 1)
{
//System.out.println("here"+j+","+k);
output++;
}
}
}
System.out.println(output);
}
}
}