-1

Need to a run a code that iterates through numbers from 2 up to - number, and gives a boolean about the input number in isPrime method. I keep getting 'true' regardless of what I input, 7 , 28, etc.

// Import statement:
import java.util.ArrayList;
class PrimeDirective {
    // Add your methods here:
    public boolean isPrime(int number) {
        int prime = 0;
        int checkPrime = number % 1;
        
        if (number == 2) {
            return true;
        }
        if (number < 2) {
            return false;
        }
        
        for (int i = 2; i < number; i++) {
            if (number % i == 1) {
                return true;
            } else {
                return false;
            }
        }
    }
    
    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));
    }
}
WJS
  • 36,363
  • 4
  • 24
  • 39
  • 1
    Side note: read about proper Java code indentation. You should put your { braces at the end of a line, and be consistent about using always the same indentation. Such things matter. They make your code easy to read, or hard to read. – Harshal Parekh Jun 24 '20 at 18:44
  • Also: please do proper research before posting a question. It is impossible that you would be the first one to ask a question on isPrime(). – Harshal Parekh Jun 24 '20 at 18:47

3 Answers3

1

First, just eliminate 2 by dividing by it first. This then allows you to check for divisibility by just odd numbers.

public boolean isPrime(int v) {
   if (v == 1 || v%2 == 0) {
       return false;
   }
   // then you just need to check for divisibility up to the
   // square root of the number.
   int max = (int)Math.sqrt(v) + 1;
   for (int i = 3; i <= max; i+=2) {
      if (v % i == 0) {
           return false;
      }
   }
   return true;
}


WJS
  • 36,363
  • 4
  • 24
  • 39
  • Im not quite clear on the math here, why the square root? – ThomasReiner Jun 25 '20 at 13:11
  • Consider some number like 19. If 19 were divisible by 2 to give `q`, then 19 would also be divisible by `q` to give 2. If by 3 to give `s` then also by `s` to give 3. So future divisions have already been caught. The point at which one can stop looking is that point when `k * k = 19`. or the next number above the square root. For 19 that would be 5. – WJS Jun 25 '20 at 13:36
  • Another way of looking at it is if any `number > sqrt(n)` divides `n` then there must be a `number < sqrt(n)` that also divides `n`. So only dividing by numbers up to the `sqrt(n)` is all that is required. – WJS Jun 25 '20 at 18:13
0

It's easier to check if a number is not prime. Your code would look like so:

for(int i = 2; i < number; i++)    
  if (number % i == 0 && number != i)
      return false;

return true;
walkman
  • 478
  • 1
  • 8
  • 21
0

You can find your Result and Optimise code if you check for only half of the number because Idealy we only need to check for square root of the number.

public boolean isPrime(int number){
 for(int i=2;i<number/2;i++)
     if(number%i==0)
          return false;
  return true;
}
  • 2
    What if `number = 1`? Your method will return `true` which is wrong as `1` is not a prime number. Also, there is no need to check up to `number/2`. Checking up to sqrt of number (`i<=Math.sqrt(number)`) is enough. Check https://en.wikipedia.org/wiki/Primality_test – Arvind Kumar Avinash Jun 24 '20 at 22:06