0

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)

Toxiro
  • 25
  • 4
  • 1
    This is a touchstone for why operator overloading is a good idea. In C++ the code for a non-built in large number type would be practically identical. Have a sympathetic upvote for having to pick a language that makes your code unreadable. – Bathsheba Feb 20 '18 at 14:44
  • 1
    @AlexisDufrenoy: Indeed. The Java guys have a curious definition of what's a duplicate. Perhaps it stems from the abominable behaviour of `==` in the language? – Bathsheba Feb 20 '18 at 15:28

1 Answers1

1

The loop with BigInteger, in your first version, should be

for(BigInteger i = n; i.compareTo(BigInteger.ZERO) == 1; i = i.subtract(BigInteger.ONE)) {
    ...
}

I can't see how your second BigInteger version could possibly do the same thing as the first one. I didn't test it, but as you are looping to the square of n, it seems really odd.

Alexis Dufrenoy
  • 11,784
  • 12
  • 82
  • 124
  • 1
    (I see… got it! thank you) – Toxiro Feb 20 '18 at 14:51
  • I'm not sure I get your problem. Does my answer solve it? – Alexis Dufrenoy Feb 20 '18 at 14:55
  • 1
    I can't test the BigInteger version on my machine (I have troubles with calling the methods in BlueJ, it always gets an exception whatever I do… I need to do it online on a testing server…) but I will let you know… thanks again! PS. that's the point of the whole thing: HOW TO MAKE the second couple of methods DO EXACTLY what the first couple does? – Toxiro Feb 20 '18 at 15:00
  • I wasn't even aware BlueJ still existed – Alexis Dufrenoy Feb 20 '18 at 15:03
  • Why the downvote? – Alexis Dufrenoy Feb 20 '18 at 15:03
  • It means somebody clicked on the down arrow, just below the "-1", near my answer, which means they disagree. I would like to know why. – Alexis Dufrenoy Feb 20 '18 at 15:11
  • I tested my code, and edited my answer. In the for, `i.subtract(BigInteger.ONE)` needs to be `i = i.subtract(BigInteger.ONE)` – Alexis Dufrenoy Feb 20 '18 at 15:11
  • Indeed. This looks correct to me, the fact that the code looks awful is down to the language, not you. – Bathsheba Feb 20 '18 at 15:31
  • 1
    @AlexisDufrenoy -> That's odd… I corrected the code as You suggested (yes, you ARE right)… but stil it can't pass the tests… @ Bathsheba -> really… the Java syntax and verbosity is a stairway to heaven… all that glitters is gold… ; ) – Toxiro Feb 20 '18 at 15:41
  • BigInteger should be avoided if you don't need it. I develop in Java since 17 years, now, and I can't remember having ever used it. – Alexis Dufrenoy Feb 20 '18 at 16:12