Интересно, есть ли способ оптимизировать эту проблему с помощью al oop (в методе findPairs), который выводит пары Integer так, чтобы числа от 1 до N и пара чисел имели сумму меньше 50 и имели более 3 общих делителей.
public class foo {
static void findPairs(int N) {
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) {
if (commDiv(i, j) > 3 && i + j < 50)
System.out.println(i + " , " + j);
}
}
}
static int gcd(int a, int b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
static int commDiv(int a, int b) {
int n = gcd(a, b);
int result = 0;
for (int i = 1; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
if (n / i == i)
result += 1;
else
result += 2;
}
}
return result;
}
}