The Prime Directive project

Hello, everyone.
I have an issue with the project. I follow every step of the project to build my code but it returns a “prime” number when it should not.

This is the input:
int numbers = {6, 29, 28, 33, 11, 100, 101, 43, 89};

This is the output:
[6, 33, 11, 101, 43, 89]
The number 33 is not a prime number.

Please, anybody could help me with this:

// Import statement:
import java.util.ArrayList;

class PrimeDirective {

// Add your methods here:
public static boolean isPrime(int number){
if(number == 2){
return true;
} else if(number < 2){
return false;
}

for(int i = 2; 2 < number; i++){
  if((number % i) != 0 ){
    return true;
  } else{
    return false;
  }
}
return false;

}

public ArrayList onlyPrimes(int numbers){
ArrayList primes = new ArrayList();

for(int number : numbers){
  if(isPrime(number)){
    primes.add(number);
  }
}
return primes;

}

public static void main(String args) {

PrimeDirective pd = new PrimeDirective();
int[] numbers = {6, 29, 28, 33, 11, 100, 101, 43, 89};

System.out.println(pd.isPrime(7));
System.out.println(pd.isPrime(28));
System.out.println(pd.isPrime(2));
System.out.println(pd.isPrime(0));
System.out.println(pd.isPrime(33));

System.out.println(pd.onlyPrimes(numbers));

}
}

Thank you!

To preserve code formatting in forum posts, see: [How to] Format code in posts

The problem lies in your isPrime method, specifically the part:

for (int i = 2; 2 < number; i++) {
    if ((number % i) != 0) {
        return true;
    } else {
        return false;
    }
}
return false;
  • Firstly, your loop condition isn’t correct. You wrote   2 < number;   whereas it should be   i < number;

  • Secondly, the logic of your if statement and the logic of your return statements isn’t correct.

In the current form, your for loop runs for only one iteration. If the number is not divisible by 2 i.e. if the number is odd, you immediately return true. Otherwise, you return false. This logic is not correct. A number being odd is not enough evidence to declare it as being a prime number (For Example, 33 and 15 are odd numbers, but they aren’t prime numbers because 33 = 3 × 11 and 15 = 3 × 5 )

Instead, one approach (there are more efficient approaches as well) is to check whether the number is divisible by any number less than itself (with the exception of 1 and the number itself),

So the loop

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

is the right idea. For example, if we want to check whether 17 is a prime number or not, then we can check whether 17 is divisible by any number in the range 2, 3, 4, ..., 15, 16. If we find that it is divisible by one of the numbers, then we should return false immediately. However, if we find that it is not divisible by one number, then we should simply move on to the next number until all the numbers in the range 2, 3, 4, ..., 15, 16 are exhausted. If we reach this point, then we have determined conclusively that the number is prime and therefore we should return true.
Consider the following version,

for (int i = 2; i < number; i++) {
    if ((number % i) == 0) {
        return false;
    }
}
return true;
  • The loop is going to iterate over all i that are less than the number.

  • If the number is divisible by some i, then it is impossible for the number to be prime and we return false immediately. We don’t need to bother checking the rest of the i.

  • If the number is not divisible by an i, then we don’t do anything and we don’t return anything yet. Instead, we let the loop start a new iteration and move on to the next i.

  • If we reach the end of the loop without having returned false, then it means: we have iterated over all the i less than the number and found that number was not divisible by any of them, therefore the number is prime. Now we can return true.

Hello.

Thank you so much! I´m glad you´ve helped me.
I understood it better with your explanation.

The code worked!

Regards!

1 Like