definition: An approximate number only has 1 and the integer itself is called Prime, or prime. computer or related majors, basically freshman began to learn programming will contact a problem is to judge prime numbers, below to share a few judgments, from normal to efficient.
1) Intuitive judgment method
The most intuitive method, according to the definition, because the prime number is no more than 1 and itself, so to determine whether N is a prime number, according to the definition of direct judging from 2 to n-1 whether there is an approximate number of n. The C + + code is as follows:
BOOL Isprime_1 (int num) { int tmp =num-1; for (int i= 2;i <=tmp; i++) if (num%i== 0) return 0; return 1;}
2) The intuitive judgment method improves the above-mentioned judgment method, obviously has the very low efficiency question. For each number of n, actually do not need to judge from 2 to n-1, we know that a number if the factorization can be decomposed, then the decomposition of the two number must be a less than or equal to sqrt (n), a greater than or equal to sqrt (n), whereby the above code does not need to traverse to n-1, traverse to sqrt (n ), because if the sqrt (n) is not found on the left, then the right side must not be able to find the approximate. The C + + code is as follows:
BOOL isprime_2 (int num) { int tmp =sqrt (num); for (int i= 2;i <=tmp; i++) if (num%i== 0) return 0; return 1;}
3) Another method
Method (2) should be the most common judgment algorithm, the time Complexity O (sqrt (n)), the speed is much faster than the method (1) O (n). Recently on the Internet by chance to see another more efficient method, for the moment called Method (3), because there is no original source, there is no link posted here, if there are original people see, please contact me, will make up the copyright reference. Here's a quick way to judge this.
let's start with a look at the distribution of prime numbers rule: Prime numbers greater than or equal to 5 must be adjacent to multiples of 6. For example 5 and 7,11 and 13,17 and 19 etc.;
Proof: To make x≥1, the natural number greater than or equal to 5 is expressed as follows: 6x-1,6x,6x+1,6x+2,6x+3,6x+4,6x+5,6 (x+1), 6 (x+1) +1 As can be seen, not in multiples of 6, that is, 6x on both sides of the number of 6x+2,6x+3,6x+4, because 2 (3x+1), 3 (2x+1), 2 (3x+2), so they must not be prime, and then remove the 6x itself, it is clear that the prime will appear only in 6x adjacent sides. Here is an off-topic, about the twin Prime, interested in the friends can learn a little bit, because it has nothing to do with our topic, skip. One thing to note here is that in multiples of 6, adjacent sides are not necessarily prime numbers. According to the above rules, judge the prime number can be 6 for the unit fast Forward, the method (2) cycle in the i++ step increase of 6, speed up the judgment speed, the code is as follows:
BOOL Isprime_3 (int num) { //two smaller number additionally handles if (num ==2| | num==3) return 1; Not on both sides of a multiple of 6 must not be prime if (num%6!= 1&&num%6!= 5) return 0; int tmp =sqrt (num); On either side of a multiple of 6 also may not be prime number for (int i= 5;i <=tmp; i+=6) if (num%i== 0| | Num% (i+ 2) ==0) return 0; Exclude all, the remaining is prime number return 1;}
Algorithm performance test: Write the test code, use more data test to compare several methods of judging efficiency, data volume 40w, the code is as follows:
#include <iostream> #include <string> #include <ctime> #include <vector>using namespace std; BOOL Isprime_1 (int num), bool isprime_2 (int num), bool isprime_3 (int num); int main () {int Test_num =40 0000; int Tstart, tstop; Record the start and end time separately//test first Judge prime number function Tstart=clock (); for (int i= 1;i <=test_num; i++) isprime_1 (i); Tstop=clock (); cout<< "Method (1) Time (ms):" <<TSTOP-TSTART<<ENDL;//ms as unit//test second Judge prime number function Tstart =clock (); for (int i= 1;i <=test_num; i++) isprime_2 (i); Tstop=clock (); cout<< "Method (2) Time (ms):" <<TSTOP-TSTART<<ENDL; Test the third judge of the prime number function Tstart=clock (); for (int i= 1;i <=test_num; i++) isprime_3 (i); Tstop=clock (); cout<< "Method (3) Time (ms):" <<TSTOP-TSTART<<ENDL; Cout<<endl; System ("pause"); return 0;}
The result of operation is as follows;
As can be seen, judging to 40w, the efficiency of the method (1) is significantly worse, the method (2) and Method (3) In this test amount of time difference of twice times more
The individual contrast methods (2) and (3), the amount of data added to 1000w, the results are as follows:
As can be seen, methods (2) and Method (3) In this test amount of time difference is still more than twice times, but it is a very good ascension. Yes, attached to the operating environment, cpu-i5-3210, memory 4g,win7,vs2012.
Well, the method of judging prime numbers is here for the time being, and the shortcomings are welcome to point out.
Judging whether a number is prime/prime--from common judgement algorithm to high efficient judgment algorithm