Are You A Prime Number

后端 未结 11 2186
甜味超标
甜味超标 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 07:49

    The time complexity of your program is O(n*m^0.5). With n the number of primes in the input. And m the size of the biggest prime in the input, or MAX_INT if you prefer. So complexity could also be written as O(n) with n the number of primes to check.

    With Big-O, n is (usually) the size of the input, in your case that would be the number of primes to check. If I make this list twice as big (for example duplicating it), it would take (+-) exactly twice as long, thus O(n).

    0 讨论(0)
  • 2021-02-10 07:51

    Your code there only has complexity O(sqrt(n)lg(n)). If you assume basic mathematical operations are O(1) (true until you start using bignums), then it's just O(sqrt(n)).

    Note that primality testing can be performed in faster-than-O(sqrt(n)lg(n)) time. This site has a number of implementations of the AKS primality test, which has been proven to operate in O((log n)^12) time.

    There are also some very, very fast probalistic tests - while fast, they sometimes give an incorrect result. For example, the Fermat primality test:

    Given a number p we want to test for primality, pick a random number a, and test whether a^(p-1) mod p = 1. If false, p is definitely not prime. If true, p is probably prime. By repeating the test with different random values of a, the probability of a false positive can be reduced.

    Note that this specific test has some flaws to it - see the Wikipedia page for details, and other probabilistic primality tests you can use.

    If you want to stick with the current approach, there are a number of minor improvements which can still be made - as others have pointed out, after 2, all further primes are odd, so you can skip two potential factors at a time in the loop. You can also break out immediately when you find a factor. However, this doesn't change the asymptotic worst-case behavior of your algorithm, which remains at O(sqrt(n)lg(n)) - it just changes the best case (to O(lg(n))), and reduces the constant factor by roughly one-half.

    0 讨论(0)
  • 2021-02-10 07:51

    A simple improvement would be to change the for loop to break out when it finds a factor:

       for (i = 2; i <= ceiling && !factorFound; i++) {
            if (input % i == 0) {
                factorFound = 1;
    

    Another possibility would be to increment the counter by 2 (after checking 2 itself).

    0 讨论(0)
  • 2021-02-10 07:54

    You can make small cuts to your algorithm without adding too much code complexity. For example, you can skip the even numbers on your verification, and stop the search as soon as you find a factor.

    if (input < 2 || (input != 2 && input % 2 == 0))
      factorFound = 1;
    
    if (input > 3)
      for (i = 3; i <= ceiling && !factorFound; i += 2)
        if (input % i == 0)
          factorFound = 1;
    

    Regarding the complexity, if n is your input number, wouldn't the complexity be O(sqrt(n)), as you are doing roughly at most sqrt(n) divisions and comparisons?

    0 讨论(0)
  • 2021-02-10 07:59

    There is no way to improve the algorithm. There may be tiny ways to improve your code, but not the base speed (and complexity) of the algorithm.

    EDIT: Of course, since he doesn't need to know all the factors, just whether or not it is a prime number. Great spot.

    0 讨论(0)
  • 2021-02-10 08:03
    #include <stdio.h>
    #include <math.h>
    
    int IsPrime (int n) {
      int i, sqrtN;
      if (n < 2) { return 0; } /* 1, 0, and negatives are nonprime */
      if (n == 2) { return 2; }
      if ((n % 2) == 0) { return 0; } /* Check for even numbers */
      sqrtN = sqrt((double)n)+1; /* We don't need to search all the way up to n */
      for (i = 3; i < sqrtN; i += 2) {
        if (n % i == 0) { return 0; } /* Stop, because we found a factor! */
      }
      return n;
    }
    
    int main()
    {
      int n;
      printf("Enter a positive integer: ");
      scanf("%d",&n);
      if(IsPrime(n))
         printf("%d is a prime number.",n);
      else
         printf("%d is not a prime number.",n);
      return 0;
    }
    
    0 讨论(0)
提交回复
热议问题