Three kinds of prime number selection method to explain (turn) __ algorithm

Source: Internet
Author: User
Tags square root

Turn from: http://tr0217.blog.163.com/blog/static/3606648020099302135503/

First: Remove 2 3 4 5 6 ... Multiples of

In the process of increasing the I from 2, the multiple of I is J*i (J is the natural number greater than or equal to 2, and the upper limit of j is the problem scale m)
To reduce the repeat step, you can remove the multiple of the number whenever I increment to equal to the first number that is not removed.
Repeat above procedure to I reach problem scale m square root +1

Three questions to be explained:
Suppose that the number of cycles to Nth is not eliminated, then the number cannot be a multiple of all the preceding digits, which is more unlikely to be a multiple of the number behind it,

Number is prime.
If the number is a composite but has not been removed, the number can be decomposed into a product of two less than the number of numbers, and the number of the front culling contains all the

Number of the product between the number, which is contradictory.

Why the first layer of the filter loop loops only to the square root of the problem scale m +1
Because, for a number of M, all the number greater than the square root of the product is greater than that number, and then remove it is redundant.

Why the second layer of the filter loop loops only to max/i.
Because j*max/i equals Max at this point, the number that needs to be marked as wrong is already up to the scale of the problem, Max, and there is no need to mark a value that is larger than Max

is a prime number, which is used to mark an array of i*j is not a prime number only max+1 capacity, which is to write data in the memory space that is not the application itself, is dangerous

of risk.

#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
const int max=1000;

int main ()
{
     int i=0,j=0,n=sqrt (MAX) +1;
     int a[max+1]={0};

     for (i=2;i<=n;i++)  //Filter loops for
       (j=2;j<=max/i;j++)
           a[j*i]=1;

      
     for (i=2;i<=max;i++)
        if (a[i]==0)
        {
          cout.width (7);
          cout<<i<< "";
        }
     System ("pause");
     return 0;
     
}


Using A[i*j] To mark I*j is not prime, this is relatively easy to think of

Take another look at the next (second, slightly optimized)

#include <stdio.h>
#include <math.h>
#define MAX_P
int nlist[max_p] = {0};
void Calc () 
{
    int n,p,t,sq= (int) sqrt (max_p*2+1);
    for (n=3;n<=sq;n+=2)
    {
        if (nlist[n>>1]) continue;
        for (t=n*n;t<=max_p<<1;t+=n<<1)//filter cycle
            nlist[t>>1] = 1;
    }
    printf ("%5d", 2);
    for (n=t=1;t<max_p;++t)
    {
        if (nlist[t]) continue;
        printf ("%5d", (t<<1) +1);
        if (++n==10)
        {
            printf ("\ n");
            N=0
}
}} int main (void)
{
    Calc ();
    GetChar ();
    return  0;
}

The method of this program is by excluding 3 5 7 9 11 ... The number of prime numbers n in the odd times to exclude primes
Mark T is not a prime by using the value of the [T/2] element in the array nlist (take 1).


1, why is the odd number of multiples.
First, let's assume that this algorithm is correct. Since the prime number is odd except 2, then every n value sent to the filter cycle is odd, excluding t when T is handed

An increase expression can be written as
for (int i=0;i<max_p/2;i++)
t=n* (n+2i)
N is odd n+2i is also odd. This is consistent with the idea of an algorithm.


2, why 3 5 7 9 11 ... The number of primes n in the number (odd number) begins with N.

That is, starting with 1 and starting with N is the same, or n before the odd multiply n can be equal to n (than n small) prime number of larger (than N-Large

) odd number of times. Because n is odd, you can set n before (n small) odd as n-2i, than n large odd for n+2j; then our mission is,

Proof N (n-2i) can be equal to m* (N+2J), where M is a prime number less than n, and I, j are positive integers. That is, n (n-m) =2mj+2ni. This has become a certificate.

Ming N-m is even, it is obvious that the difference of two odd numbers must be even.


3. Why the filtering cycle excludes all the primes.

Whether the algorithm is correct or not, 3 5 7 9 11 ... The odd number of times of any n in the is excluded some composite numbers.
First we assume that n loops to a certain n when the composite number is not eliminated, then n can decompose the factor into several primes and is odd (since N is a 3-odd

Number of the product, of course, also can be expressed as a prime A and an odd B of the product form, then this is necessarily less than N of the prime. The odd number of times this primes

Must have been ruled out in the previous cycle. This is not excluded from the contradictions. So the algorithm is correct


Finally, let's take a look at the third Kind

#define MAX_N 1000
int a[max_n+1];
int p[max_n+1];
int ncount=0;

void Init (int n)//linear sieve, but on a small scale (approximately n<1e7) is no faster than the previous method
{for
    (int i=2;i<=n;i++)
    {
        if (a[i]==0)
        {
            p[++ncount]=i;
        }
        for (int j=1,k; (J<=ncount) && (K=i*p[j]) <=n; J + +)//filter cycle
        {
            a[k] = 1;
            if (i%p[j] = = 0) break
        ;

}} #include <iostream>
int main (void)
{
    Init (max_n);
    for (int n=1; p[n]>1; ++n)
    {
        printf ("%5d", P[n]);
    return 0;
}

This one can be said to be a direct distortion of the previous algorithm
Using A[k]=1 to mark K is not prime
The first is to exclude composite numbers with a multiple of the correct number (that is, prime numbers) filtered out

The second is to use 2 to n multiplication to filter out the correct number, that is, prime

If you disagree, I can put for (n=3;n<=sq;n+=2) as (n=3;n<sq;n++)
It's close to the first algorithm, and since the first and second are proven correct, then this one is also true.
If it can be proved that if (i%p[j] = = 0) Break, it is reasonable to add this sentence.

This sentence can be explained as when I first became the number of the correct sequence of numbers (set to n) of the selected primes, there is no need to let I go

Multiply the number of primes in the sequence of ascending prime numbers, which can be equivalent to an integer (set to max) that can be equal to the number of the appropriate prime number for the I multiplication ratio n larger than the size of I (set

To j) Multiply the prime number (set to min) smaller than N, and this j is not an integral multiple of M, i.e. i*max=j*min;
and can be equivalent to J=i*max/min is an integer that is not a min multiple, based on previous experience of factoring an integer must be decomposed into an increment

The product of the sequence of prime numbers, if we assume that I*max is such an integer, Max is a slightly larger number in the factor increment sequence, then min as long as it is in the increment sequence

A prime number less than Max can make J an integer, and obviously Min is contained in all primes less than Max, and Nature J is an integer,
And since I is not a multiple of Max, Max is not a multiple of min (if yes, Max is a prime number). Then I must be a multiple of min, and I is the first time into

Integer times of a number (set to n) of an ascending sequence for the selected correct primes, I must not be a multiple of min squared, that is, I/min is not a min.

Number, I/min*max is not a multiple of min
This proves that if (i%p[j] = = 0) break; this sentence is reasonable to add

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.