Why can't the program pass these assertions?

Huskii :

My code: (class name is math and it implements an interface)

public boolean isPrime(int n){
    for (int i=2; i<n; i++){
        if (n%i==0){
            return false;
        }
    }
    return true;
}

Assertions needed to pass:

assert math.isPrime(2);
assert math.isPrime(3);
assert math.isPrime(53);
assert !math.isPrime(55);
assert !math.isPrime(24);
assert !math.isPrime(-37337);

Oddly enough I've found that the method will pass the -37337 assertion by changing my code to:

for (int i=2; i<n; i++){

          if (!(n%i==0)){
              return true;
          }

      }
      return false;

But I can't seem to figure out how to pass all of the assertions

Michael Bianconi :

Because it never enters the loop (2 is greater than -37337)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=169670&siteId=1