[Primes determines - from high to violence] -C ++

Today we talk about the determination of prime numbers.
For each OIer, in the long course of practice, is not a prime number can not appear in our eyes, it is determined that every prime number is a OIer should have the operation, so we share today from several violent methods to determine efficient.


1. Method intuitive judgments

Because this method is actually what we usually say violence law. The definition of primes, the number can not be divisible by integer n in the 2 ~ n-1 is called a prime number. So we went from 2 n-1, each can be determined modulo, which is most intuitive to a method, 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. Optimization intuitive judgments

The above-described determination method, the presence of significant low efficiency. For each number n, in fact, does not need determination from 2 to n-1, we know that, if a number can be factorized, both operands must be obtained when a less exploded sqrt (n), is greater than a is equal to sqrt (n), whereby said code does not need to traverse to n-1, to traverse sqrt (n) can be, because the number can not be found on the left side when about sqrt (n), it must also find the right less than a few about! So I went from 2 sqrt (n) on it. code show as below:

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 way (thought the name)

This way I also forget've seen in a blog on which, if bloggers see copyright contact me to make reference to
first look at a law on the distribution of primes: prime number greater than or equal to some 5 and 6 of the adjacent multiples . 5, for example, 7, 11 and 13, 17 and 19 and the like;

Proof: Let x≥1, a natural number greater than or equal to 5 is represented as follows:
······ 1,6x-6X, 6X + 1,6x + 2,6x + 3,6x + 5,6 + 4,6 x ( x + 1), 6 (x + 1) +1 ······

This clearly shows that, on both sides is not a multiple of 6, i.e., the number of sides of 6x 6x + 2,6x + 3,6x + 4, since 2 (3x + 1), 3 (2x + 1), 2 (3x + 2), so they must not prime , then removed 6x itself, obviously, the prime number to appear adjacent to both sides of it comes from the 6x . The point to note here is that two adjacent sides in a multiple of 6 and is not necessarily is a prime number.

At this time, determined prime number can fast-forward to 6 as a unit , i.e. a method (2) loop i ++ step increase of 6, to accelerate the determination speed, the reason is that if you want determined number n, then n must be 6x-1 or 6x + 1 in the form of, for circulating 6i-1,6i, 6i + 1,6i + 2,6i + 3,6i + 4, where n can if 6i, 6i + 2,6i + 4 divisible, then at least n We have an even number, but in the form of 6x-1 or 6x + 1 is an odd obviously, it is not established; Further, if n is divisible be 6i + 3, n is at least 3 can be divisible, but 6x divisible by 3, so 6x-1 or 6x + 1 (i.e. n) can not be divided by 3, which is not established. In summary, the loop need only consider the case 6i-1 and 6i + 1's , i.e., the cycle can be defined as the step 6 , each loop variable determining k and k + 2 the situation can.

Code implementation is very simple, but it should be noted that there are two situations need special sentence:

1. this number is 1, it is necessary to return to false;
2. this number is 2 or 3, need to return true;

Other accordance with the above ideas to break out on the right, the code is as follows:

bool isPrime_3(int num)
{
if(num==1)
return 0;
if(num==2||num==3)
return 1;
if(num%6!=1&&num%6!=5)
return 0;
int tmp=sqrt(num);
for(int i=5;i<=tmp;i+=6)
if(num%i==0||num%(i+2)==0)
return 0;
return 1;
}

Next, when used to test;
note: when used below refers to the determination from 1 ~ n are not a prime number, it is not determined again!
Then transferred to 40W on the first data range;
operating results:

Here Insert Picture Description

Obviously, the process is too slow! but! Although the method is very fast, but time-consuming method is still more than three times 3! Fast enough to prove that the third method.

That methods 2 and 3 compared separately Next, the n 1000w transferred to try,
the results:
Here Insert Picture Description
data to 1000w, when the method 3 is fully exhibit its advantages, this method of determining the prime number is very economical when used He got time with.

Read the above method, were used to try over the question head:

P3383 [template] linear sieve prime number

ov.

Guess you like

Origin www.cnblogs.com/moyujiang/p/11235653.html