Reference article: http://blog.csdn.net/kp_liu/article/details/37569507
http://blog.csdn.net/huang_miao_xin/article/details/51331710
Https://www.zybang.com/question/93c4703c84c5ad3c1c34b1e6672b0568.html
Prime Number
--only the number that is divisible by 1 and itself (except 1, 1 is neither prime nor composite )
-
-
-
-
-
-
- Now give a number n (n is a positive integer), write a method to determine whether it is a prime.
Intuitive ideas
Traverse from 2 to N-1 to see if there are approximations of N. If present, it is not a prime number.
Basic improvements
Do not have to traverse from 2 to N-1, the number n if composite, it can be at least two factors multiplied, the two number must be a greater than (or equal to) sqrt (n), a less than (or equal to) sqrt (n), so only need to traverse to sqrt (n).
Improvement Method one (even; narrow judgment range;)
Step 1 (except 2) determines whether n is an even number, and if so, the end of judgment-not a prime number;
Step 2 If n is not even but composite, its factor can never be an even number ( cause even x even = even), even x odd = even),
Therefore, when judging the factor of odd N, we can omit the judgment of the dual number. (i+=2)
1 Static BooleanIsprime_1 (intN) {2 intTmp= (int) math.sqrt (N);3 //Step 14 if(n==2)return true;5 if(n%2==0)return false;6 //Step 27 for(inti=3;i<tmp;i+=2)8 if(n%i==0)return false;9 Ten return true; One}
Improvement Method Two (prime number distribution law; pruning strategy;)
A rule about the distribution of prime numbers: prime numbers greater than or equal to 5 must be adjacent to multiples of 6 (that is, prime number x greater than or equal to 5), which can be expressed as x=6k+1 or X=6k-1,k to >0.
Note, however, that in multiples of 6, the adjacent sides are not necessarily prime numbers.
Prove
Intuitively, the natural number greater than or equal to 5 can be expressed in 6k-1,6k,6k+1,6k+2,6k+3,6k+4,6k+5 form, obviously 6k+2 = 2 (3k+2), 6k+3 = 3 (2k+1), 6k+4 = 2 (3k+2) are composite, and then remove 6k itself, Prime numbers greater than or equal to 5 can therefore be represented as 6k+1 and 6k+5 (when x is greater than or equal to 6 o'clock, 6k+5 and 6k-1 can represent the same number).
Mathematical proofs
- Prime number x is greater than or equal to 5, then it must not be a multiple of 3, nor a multiple of 2.
- If a natural number is not a multiple of 3, there are two cases, set x=3p+1 or x=3p+2 (p>1)
- If a natural number is not a multiple of 2, there is a case, set x=2t+1 (t>3)
1) if x=3p+1, and x=2t+1
Then x=3p+1=2t+1 >> x-1=3p=2t
>> 3p=2t, p is a multiple of 2, T is a multiple of 3,
Set P=2k,>> t=3k
So x=2*3k+1=6k+1
2) if x=3p+2, and x=2t+1
Then x=3p+2=2t+1
>> 3p+1=2t, so the 3p is odd, that is, p is odd, set p=2k-1
Then (2k-1) +1=2t,>> t=3k-1
So x=2 (3k-1) +1=6k-1
Step 1 alone Judge 2, 3, are prime numbers;
Step 2 Determines whether n is on either side of a multiple of 6, if not, the end of judgment-not a prime number;
Step 3 N in multiples of 6 are not necessarily prime numbers, but also to determine whether the number of the two sides of the 6 can be found in a number of n factor, if not found, can be judged as prime.
(Why do I need to look for a factor in the "number of multiples of 6"?)
cause All composite can consist of the product of prime numbers, so n only needs to be taken with numbers that may be prime numbers (that is, two numbers before and after a multiple of 6)
1 Static BooleanIsprime_2 (intN) {2 intTmp= (int) math.sqrt (N);3 //Step 14 if(n==2| | N==3)return true;5 //Step 26 if(n%6!=1&&n%6!=5)return false;7 //Step 38 for(intI=5;i<=tmp;i+=6)9 if(n%i==0| | N% (i+2) ==0)return false; Ten /*here One * I is expressed as a number of 6k+5, A * i+2 representation of 6k+1 - */ - the return true; -}
Method Comparison && code testing
Test code
Data conditions
-
-
-
-
-
-
- "Improvement method Two" outright
E N D
Algorithmic Improvements | Determining prime numbers in the Java language