You should not pass a,b,c
to the method, they are just local variables, but you may pass the max
. Then you don't need to iterate for c
, just compute it from a
and b
private static void greatestCommonFactor(int max) {
for (int a = 1; a <= max; a++) {
for (int b = a; b <= max; b++) {
double c = Math.sqrt(a * a + b * b);
if (c == Math.floor(c)) {
System.out.println(a + " " + b + " " + (int) c);
}
}
}
}
Call as greatestCommonFactor(50);
You can change the condition to if (c == Math.floor(c) && c < max)
is you prefer
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…