Is there a simple algorithm that can determine if X is prime?

后端 未结 16 1496
离开以前
离开以前 2020-12-01 00:32

I have been trying to work my way through Project Euler, and have noticed a handful of problems ask for you to determine a prime number as part of it.

  1. I kno

16条回答
  •  天命终不由人
    2020-12-01 00:42

    Maybe this implementation in Java can be helpful:

    public class SieveOfEratosthenes {
    
        /**
         * Calling this method with argument 7 will return: true true false false true false true false
         * which must be interpreted as : 0 is NOT prime, 1 is NOT prime, 2 IS prime, 3 IS prime, 4 is NOT prime
         * 5 is prime, 6 is NOT prime, 7 is prime.
         * Caller may either revert the array for easier reading, count the number of primes or extract the prime values
         * by looping.
         * @param upTo Find prime numbers up to this value. Must be a positive integer.
         * @return a boolean array where index represents the integer value and value at index returns
         * if the number is NOT prime or not.
         */
        public static boolean[] isIndexNotPrime(int upTo) {
            if (upTo < 2) {
                return new boolean[0];
            }
    
            // 0-index array, upper limit must be upTo + 1
            final boolean[] isIndexNotPrime = new boolean[upTo + 1];
    
            isIndexNotPrime[0] = true; // 0 is not a prime number.
            isIndexNotPrime[1] = true; // 1 is not a prime number.
    
            // Find all non primes starting from 2 by finding 2 * 2, 2 * 3, 2 * 4 until 2 * multiplier > isIndexNotPrime.len
            // Find next by 3 * 3 (since 2 * 3 was found before), 3 * 4, 3 * 5 until 3 * multiplier > isIndexNotPrime.len
            // Move to 4, since isIndexNotPrime[4] is already True (not prime) no need to loop..
            // Move to 5, 5 * 5, (2 * 5 and 3 * 5 was already set to True..) until 5 * multiplier > isIndexNotPrime.len
            // Repeat process until i * i > isIndexNotPrime.len.
            // Assume we are looking up to 100. Break once you reach 11 since 11 * 11 == 121 and we are not interested in
            // primes above 121..
            for (int i = 2; i < isIndexNotPrime.length; i++) {
                if (i * i >= isIndexNotPrime.length) {
                    break;
                }
                if (isIndexNotPrime[i]) {
                    continue;
                }
                int multiplier = i;
                while (i * multiplier < isIndexNotPrime.length) {
                    isIndexNotPrime[i * multiplier] = true;
                    multiplier++;
                }
            }
    
            return isIndexNotPrime;
        }
    
        public static void main(String[] args) {
            final boolean[] indexNotPrime = SieveOfEratosthenes.isIndexNotPrime(7);
            assert !indexNotPrime[2]; // Not (not prime)
            assert !indexNotPrime[3]; // Not (not prime)
            assert indexNotPrime[4]; // (not prime)
            assert !indexNotPrime[5]; // Not (not prime)
            assert indexNotPrime[6]; // (not prime)
            assert !indexNotPrime[7]; // Not (not prime)
        }
    }
    

提交回复
热议问题