user10100235
user10100235

Reputation:

Getting weird output while finding prime number in java

I have two methods to find out prime number in java method - 2 working fine but getting wrong output from method one, can any help me where i did wrong in logic. Thanks in advance

My entire code

package prepare;

import java.util.Scanner;

    public class Squar {
        //Method - 1 to find prime number
        boolean isPrime(int num){
            int exp = (int)Math.sqrt(num);
            for(int i=2;i<exp;i++){
                if(exp%2==0){
                    return false;
                }
            }return true;
        }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int num = scan.nextInt();
        Squar s = new Squar();
        System.out.println("From M1 "+s.isPrime(num));
        scan.close();
        System.out.println("From M2 "+s.isPrimeNumber(num));
    }
    //Method - 2 to find prime number
    public  boolean isPrimeNumber(int number) {
        if(number == 1){
            return false;
        }
        if (number == 2 || number == 3) {
            return true;
        }
        if (number % 2 == 0) {
            return false;
        }
        int sqrt = (int) Math.sqrt(number) + 1;
        for (int i = 3; i < sqrt; i += 2) {
            if (number % i == 0) {
                return false;
            }
        }
        return true;
    }

}

for input : 63 actual out put would be false in prime number but getting different output from method one output

63
From M1 true
From M2 false

Upvotes: 0

Views: 213

Answers (5)

Shinchan
Shinchan

Reputation: 361

Well I think the culprit is if(exp%2==0){ and it is causing a problem while iterating i<exp.So you may want to tweak it to num%i==0

I have tried to give a few other approaches to this issue. I hope that would be helpful.

I think there is a reason that tempted you to use
(int)Math.sqrt(num);

I have tried to elaborate it below.

Consider below 3 approaches. All of them are correct but the first 2 approaches have some drawbacks.

Approach 1

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

We have a scope to make it faster.

Consider that if 2 divides some integer n, then (n/2) divides n as well. This tells us we don't have to try out all integers from 2 to n.

Now we can modify our algorithm:

Approach 2

//checks whether an int is prime or not.
boolean isPrime(int num) {
for(int i=2;2*i<num;i++) {
    if(num%i==0)
        return false;
}
return true;
}

Finally, we know 2 is the "oddest" prime - it happens to be the only even prime number. Because of this, we need only check 2 separately, then traverse odd numbers up to the square root of n. I think this might have tempted you to use (int)Math.sqrt(num);

Approach 3

//checks whether an int is prime or not.
boolean isPrime(int num) {
//check if num is a multiple of 2
if (num%2==0) return false;
//if not, then just check the odds
for(int i=3;i*i<=num;i+=2) {
    if(num%i==0)
        return false;
}
return true;
}

Hence, we've gone from checking every integer (up to n to find out that a number is prime) to just checking half of the integers up to the square root. Is it not an improvement, especially considering when numbers are large.

Upvotes: 1

Rachit Tayal
Rachit Tayal

Reputation: 1292

Have a look at this line of your code

if(exp%2==0){

it should be num % i

Upvotes: 1

drowny
drowny

Reputation: 2147

Change isPrime function like this.

 boolean isPrime(int num) {
        int exp = (int) Math.sqrt(num);
        for (int i = 2; i < exp; i++) {
            if (num % i == 0) {
                return false;
            }
        }
        return true;
    }

Because in if condition you are checking exp%2 == 0 . But this statement does not change when iterating on i < exp. So this logic should be on with num % i == 0

Upvotes: 1

Adam Ostrožl&#237;k
Adam Ostrožl&#237;k

Reputation: 1416

Well, your first algorithm is almost (replace %2 with %i) correct. I do not know the second algorithm, but i would definitely change it to this form:

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

Upvotes: 0

Mushif Ali Nawaz
Mushif Ali Nawaz

Reputation: 3866

In isPrime() method, Shouldn't you be checking num % i == 0 rather than exp % 2 == 0?

Upvotes: 2

Related Questions