public static boolean isPrime(int number) {
        // For performance, check if the number is divisable by severl of
        // the smaller prime numbers.  Note that a prime is divisible by
        // itself, so check for the smaller primes prior to performing
        // the divion check.
        if (number == 2 || number == 3 || number == 5 || number == 7 ||
            number == 11 || number == 13 || number == 17 || number == 19)
            return true;

        if (number % 2 == 0 || number % 3 == 0 || number % 5 == 0 || number % 7 == 0 ||
            number % 11 == 0 || number % 13 == 0 || number % 17 == 0 || number % 19 == 0)
            return false;

        // Now perform the Euler's criterion check for the bases 2, 3, and 5. This is
        // valid only for odd numbers, so the even must be filtered out above.
        long numberMinusOne = number - 1;
        long s = 0;
        long d = numberMinusOne;

        while (isEven(d)) {
            d = d / 2;
            ++s;
        }

        long d1 = d;

        for (int a = 2; a <= 5; ++a) {
            if (a == 4)
                continue; // Skip the base 4.

            long prod = 1;
            long a2j = a;
            d = d1;
            while (d > 0) {
                if (isOdd(d))
                    prod = (prod * a2j) % number;

                d = d / 2;
                a2j = (a2j * a2j) % number;
            }

            // prod = a^d mod number has been calculated.

            if (prod == 1 || prod == (numberMinusOne))
                continue;

            for (long i = 1; i <= s; ++i) {
                prod = (prod * prod) % number;
                if (prod == (numberMinusOne))
                    break;
            }

            if (prod != (numberMinusOne)) {
                return false;
            }
        }

        // There are a few non-prime numbers below 25x10^9 that pass Euler's
        // criterion, so check for them here.  Note that severl of them are larger
        // than an integer, but they are commented out here for completeness.
        if (number == 25326001 || number == 161304001 ||
            number == 960946321 || number == 1157839381)
            // 3215031751, 3697278427, 5764643587, 6770862367, 14386156093,
            // 15579919981, 18459366157, 19887974881, 21276028621
            return false;

        return true;
    }
public static boolean isPrime ( int number ) { if ( number == 2 || number == 3 || number == 5 || number == 7 || number == 11 || number == 13 || number == 17 || number == 19 ) return true ; if ( number % 2 == 0 || number % 3 == 0 || number % 5 == 0 || number % 7 == 0 || number % 11 == 0 || number % 13 == 0 || number % 17 == 0 || number % 19 == 0 ) return false ; long numberMinusOne = number - 1 ; long s = 0 ; long d = numberMinusOne ; while ( isEven ( d ) ) { d = d / 2 ; ++ s ; } long d1 = d ; for ( int a = 2 ; a <= 5 ; ++ a ) { if ( a == 4 ) continue ; long prod = 1 ; long a2j = a ; d = d1 ; while ( d > 0 ) { if ( isOdd ( d ) ) prod = ( prod * a2j ) % number ; d = d / 2 ; a2j = ( a2j * a2j ) % number ; } if ( prod == 1 || prod == ( numberMinusOne ) ) continue ; for ( long i = 1 ; i <= s ; ++ i ) { prod = ( prod * prod ) % number ; if ( prod == ( numberMinusOne ) ) break ; } if ( prod != ( numberMinusOne ) ) { return false ; } } if ( number == 25326001 || number == 161304001 || number == 960946321 || number == 1157839381 ) return false ; return true ; }
Returns true if the specified integer is a prime number. <p>

This method uses an algorithm to determine the primality of a number,
adapted from a sample program in the book "Prime Numbers and Computer
Methods for Factorization" by Hans Riesel, 1985, and is based on
Euler's criterion for quadratic residues. The algorithm is valid
for any number below 25x10^9 (which is only slightly larger than the
maximum integer value). <p>

Note that any adaptation of this algorithm for long values needs to
take into account the limit for which it is valid.