Are You A Prime Number

后端 未结 11 2177
甜味超标
甜味超标 2021-02-10 07:39

I\'ve been interested in the problem of finding a better prime number recognizer for years. I realize this is a huge area of academic research and study - my interest in this i

11条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-10 08:05

    The time complexity for each primality test in your algorithm is O(sqrt(n)).

    You can always use the fact that all primes except 2 and 3 are of the form: 6*k+1 or 6*k-1. For example:

    int is_prime(int n) {
      if (n <= 1) return 0;
      if (n == 2 || n == 3) return 1;
      if (n % 2 == 0 || n % 3 == 0) return 0;
      int k;
      for (k = 6; (k-1)*(k-1) <= n; k += 6) {
        if (n % (k-1) == 0 || n % (k+1) == 0) return 0;
      }
      return 1;
    }
    

    This optimization does not improve the asymptotic complexity.

    EDIT

    Given that in your code you are testing numbers repeatedly, you might want to pre-calculate a list of primes. There are only 4792 primes less than or equal to the square root of INT_MAX (assuming 32 bit ints).

    Furthermore, if the input numbers are relatively small you can try calculating a sieve.

    Here's a combination of both ideas:

    #define UPPER_BOUND 46340  /* floor(sqrt(INT_MAX)) */
    #define PRIME_COUNT 4792  /* number of primes <= UPPER_BOUND */
    
    int prime[PRIME_COUNT];
    int is_prime_aux[UPPER_BOUND];
    
    void fill_primes() {
      int p, m, c = 0;
      for (p = 2; p < UPPER_BOUND; p++)
        is_prime_aux[p] = 1;
      for (p = 2; p < UPPER_BOUND; p++) {
        if (is_prime_aux[p]) {
          prime[c++] = p;
          for (m = p*p; m < UPPER_BOUND; m += p)
            is_prime_aux[m] = 0;
        }
      }
    }
    
    int is_prime(int n) {
      if (n <= 1) return 0;
      if (n < UPPER_BOUND) return is_prime_aux[n];
      int i;
      for (i = 0; i < PRIME_COUNT && prime[i]*prime[i] <= n; i++)
        if (n % prime[i] == 0)
          return 0;
      return 1;
    }
    

    Call fill_primes at the beginning of your program, before starting to process queries. It runs pretty fast.

提交回复
热议问题