Prime Number Detection Algorithm

Source: Internet
Author: User
Preface when I was doing ACM today, I encountered a prime number check. When I checked a prime number in a range, if I used the simplest method, the timeout would be serious, so I learned a new algorithm for prime number detection-the prime number embedding method. Here I will share with you the prime number.

A prime number defines an integer greater than 1. If it cannot be divisible by any positive integer other than 1, it is an integer greater than 1 defined by a prime number (also called prime number, if it is not a prime number, it is a combination of numbers. The positive integer in which the number can be divisible is called an approximate number, and the positive integer not equal to 1 is not equal to the Union itself. Note: 1 is neither a prime number nor a combination of numbers to detect a prime number. The so-called prime number detection means to give an integer greater than 1 to determine whether the integer is a prime number. The factor detection method is to try from 2 to n-1 to check whether N can be divisible. If N can be an integer N (locate a factor), n is not a prime number, otherwise, n is considered as a prime number. Actually, there is no need to test n-1, as long as the second root of N is enough, the reason is: set n = a * B, and A and B are non-ordinary n approx, obviously, the quadratic root of a> N and the quadratic root of B> N cannot be established at the same time, because a * B will be greater than N at the same time, so if n has an extraordinary approximate number, there must be at least one quadratic root less than or equal to N. Therefore, you only need to traverse the quadratic root of N. The implementation code of factor detection is as follows (c ):
int isPrime(int n){int i, flag;flag = (n <= 1)? 0 : 1;for(i = 2; i <= sqrt(n); i ++){if(n % i == 0){flag = 0;break;}}return flag;}
Test results: time complexity: Obviously, the time complexity of the factor detection algorithm is 0 (the second root of N). In general, this time complexity is already very good, however, if I want all prime numbers within N, the time complexity will instantly reach the second root of N * n, this is an intolerable prime number for N when it is very large. The prime number embedding method uses the prime number factor detection method to find all prime numbers within n. the time complexity is too high to be tolerated. Therefore, another algorithm is introduced here, the prime number embedding method can save a lot of time. The direct effect is that I instantly AC the question about prime number detection on the 9du OJ, and Wa is used for prime number factor detection. Principle: When I is a prime number, all multiples of I must be a Union number. If I has already determined that it is not a prime number, find the prime number behind I to screen out the multiples of this decision tree. Filtering Process for prime numbers within 20: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 17 18 19 20
  • Step 1, because all the even numbers and 0, 1 must not be prime numbers, mark the values of these numbers as flase, And the rest as true.
  • Step 2:
  1. I = 3. Because Prime [3] = 1, prime [6], prime [9], prime [12], prime [15], prime [18] is marked as 0;
  2. I = 4. Since Prime [4] = 0, it will continue without processing;
  3. I = 5> SQRT (20), algorithm end
Time Complexity: Due to my limited mathematical knowledge, I cannot explain the computing process. In short, it is indeed very fast. As for why I = 5 is over, the reason is the same as the principle in the prime factor method, because the factor of N must have a quadratic root less than or equal to n. Implementation Code of the prime number embedding method:
Void getprimearray (int * prime) {int I, j; // initialize the prime number identification array for (I = 0; I <Max; I ++) {If (I = 2 | I % 2! = 0) & I! = 1) {Prime [I] = 1 ;}else {Prime [I] = 0 ;}// filter prime numbers for (I = 2; I * I <Max; I ++) {If (prime [I]) {for (j = 2 * I; j <Max; j + = I) {Prime [J] = 0 ;}}}}

Zhang Yang's blog-a technical cool man I admire very much in http://www.codinglabs.org/html/prime-test.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.