One very fast method of Rejecting a trial prime is to use that fact that all primes are of the form 6k +/- 1, where k is a positive integer. (2 and 3 excepted).
This means you can reject a number with only two tests (plus the trivial).
public static boolean checkPrime(long p)
{
if (p==2 || p==3) return true;
if ( (p+1) % 6 == 0 || (p-1) % 6 == 0 )
{
// p might be prime, worth checking
return checkProbablePrime(p);
}
else
{
// p is not prime.
return false;
}
}
Whether or not this is a good technique depends on the distribution of inputs. If you expect a high percentage of composite numbers, then this is a quick way of eliminating them from contention. It would be nice to eliminate the trivial branch at the top (==2, ==3) but that depends on the context of the inputs. If your goal is to FIND primes, then you should skip this.