Prime Directive - not returning prime number

Hi all,

After working on Prime Directive for a frustrating 2 weeks I am returning a list of prime numbers but included in the list is 33 which is not a prime number. I can’t figure out why 33 is included in the list.

I will include the method here for isPrime:

public boolean isPrime(int number) {

if(number == 2){

 return true;

} else if (number < 2){

 return false;

}

// check divisibility here

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

  if (number % i == 0){

  return false;

  }

  return true;

}
It returns as follows:
image

Remove return true; from your for loop, and place it at the end of the method instead; it seems to be returning prematurely:

public static boolean isPrime(int number) {
  if(number == 2){
    return true;
  }else if (number < 2){
    return false;
  }
  // check divisibility here
  for (int i = 2; i < number; i++) {
    if (number % i == 0){
      return false;
    }
  }
  return true;
}
2 Likes

Brilliant ! :slight_smile:
Thankyou so much

1 Like