How to implement an efficient code in java to verify whether a given number is a prime number or not for number's of size greater than or equal to 12 digits?
Example
Input:
100123456789
Output: prime
Input:
101111111111
Output: prime
Input:
101740496633
Output: prime
Input:
111111111111
Output:not prime
Input:
157639024808
Output: not prime
I tried implementing the following algorithm to find whether it is prime or not. But it is not working taking too long for numbers with greater than or equal to 12 digits.
My Code
public static Boolean checkPrime(long a){
if(a%2==0)
return false;
for(int i=3;i<(int)Math.sqrt(a);i=i+2){
if(a%i==0){
return false;
}
}
return true;
}
'a' is the number to be checked
The above function returns true if the given number is prime or false if the given number is not prime.
How to find whether a given number is prime or not for numbers of size greater than 12?