0

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

1
  • 1
    Note copied from wikipedia: "A prime number (or a prime) is a natural number greater than 1 that cannot be formed by multiplying two smaller natural numbers...." - code is not testing for greater than 1! Commented Mar 9, 2020 at 19:00

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.