I would need to know if and how these two for loop methods are matching, that according to their own syntax (the two methods with 'integer' type are correct and tested, but the two analogues with 'BigInteger' type are not). Are the two methods (of BigInteger type) the exact equivalent of the ones basing on the integers? Where's the trick?
FOR-LOOP METHODS (INTEGERS):
public static int greatest(int x, int y, int n){
for(int i = n; i > 0; i--) {
n -= 1;
if (n % x == 0 && n % y == 0){
break;
}
}
return n;
}
public static int smallest(int x, int y, int n){
for(int i = 0; i < n*n; i++) {
n += 1;
if (n % x == 0 && n % y == 0){
break;
}
}
return n;
}
VS FOR-LOOP METHODS (BIGINTEGERS):
public static BigInteger greatest(BigInteger x, BigInteger y, BigInteger n){
for(BigInteger i = n; i.compareTo(BigInteger.ONE) == 0; i = i.subtract(BigInteger.ONE)) {
n = n.subtract(BigInteger.ONE);
if (n.mod(x) == BigInteger.ZERO && n.mod(y) == BigInteger.ZERO){
break;
}
}
return n;
}
public static BigInteger smallest(BigInteger x, BigInteger y, BigInteger n){
for(BigInteger i = BigInteger.ZERO; i.equals(n.multiply(n)); i = i.add(BigInteger.ONE)) {
n = n.add(BigInteger.ONE);
if (n.mod(x) == BigInteger.ZERO && n.mod(y) == BigInteger.ZERO){
break;
}
}
return n;
}
(your suggestions are more than welcome, thank you)