private static boolean IsPrime(long number, List<Long> primes) {
        if (number < primes.get(primes.size() - 1)) {
            return 0 <= Collections.binarySearch(primes, number);
        } else {
            for (int i = 0; i < primes.size(); i++) {
                long prime = primes.get(i);
                if (number % prime == 0) {
                    return false;
                } else if (number < prime * prime) {
                    return true;
                }
            }

            for (long i = primes.get(primes.size() - 1) + 2; i * i <= number; i += 2) {
                if (number % i == 0) {
                    return false;
                }
            }
            return true;
        }
    }