Prime number Screening Method __ Algorithm Introduction Series

Source: Internet
Author: User

There are two kinds of prime number screening, one is the normal filter, O (NLOGNLOGN)

The basis is:

1. If x is a prime number, then the multiples of X are not prime
2. If x is not a prime number, then X is definitely filtered out between [1,x]. That is, the presence of Y, making k*y=x, and then y according to inference 1, determines that X is a non prime.
= "X is prime if x has not been filtered out beforehand"

By the current to filter the back of the

#include <cstdio>
#include <string>
#include <iostream>
using namespace std;
const int MAXN = 10000050;

The prime number  O (NLOGLOGN)
int PRIME[MAXN] = {0};//0 is prime, and 1 is a non prime
void getprime (int n)
{
    int i;
    int t;
    for (i = 2; I * I <= n; i++)
    {
        if (!prime[i]) {for
            (t = 2 * i; t <= n; t = i)
            {
                prim E[t] = 1;
            }}}


The second is linear filtering, approximate to the complexity of O (n)

Linear screening is  approximate to O (n)
bool ISP[MAXN];     Whether ISP[I]:I is prime, 0/false is prime
int P[MAXN];        Stored prime
int pnum=0;
void getPrime2 (int n)
{for
    (int i = 2; I <= n; i++)
    {
        if (!isp[i])    p[pnum++] = i;
        for (int j = 0; J < pnum && I * p[j] <= N; j +)
        {
            isp[i * p[j]] = 1;      The I-p[j of all prime numbers before I is sifted out
            if (!) ( I% p[j]))              ///If I divide some p[j], stop the break
                ;}
    }

Quick on the processing of the I = composite

Presumably the idea is for a number I (prime or composite), sift out the I-fold of all prime numbers of J, which is less than this number, when J is a divisor of I (I is a multiple of J, at this time I is certainly for composite numbers) no longer screened (j+1) * I, (j+2) * I ... )

Because: the product of a composite sum and a prime number can be expressed as a product of a larger sum and a smaller one (any sum can be equal to the product of several prime numbers, and the product of two prime numbers is a composite.) The original composite number is decomposed into a smaller prime number x and a prime y, at which point the product of the prime number of the Y-multiplication becomes a larger composite number.

If I = 15 o'clock, the prime number before 15 is 2,3,5,7,11,13 then sift out the 2*15 after the discovery of 15%3==0, then the 15*3 also sieve off, no longer screening 15*7,15*11,15*13.

For example 15*7: Because 15=3*5, then 15*7 = = 3*5*7 = = 35 * 3 "I = 35 o'clock Filter j = 3" Found I = 35 o'clock will screen out 35*3, so now do not screen 15*7.


It turns out that the data below 100W, the general screening is better than the linear filter.


Complete test Code

#include <cstdio> #include <string> #include <iostream> using namespace std;

const int MAXN = 10000050; Prime number O (NLOGLOGN) int PRIME[MAXN] = {0}, within normal filter n;
    0 denotes prime number, 1 is a non prime number void Getprime (int n) {int i;
    int t;
            for (i = 2; I * I <= n; i++) {if (!prime[i]) {for (t = 2 * i; t <= n; t = i)
            {Prime[t] = 1;     ()}}//Linear filter is approximate to O (n) bool ISP[MAXN];        Whether ISP[I]:I is prime, 0/false is prime int P[MAXN];
stored prime int pnum=0;
        void getPrime2 (int n) {for (int i = 2; I <= n; i++) {if (!isp[i]) p[pnum++] = i;      for (int j = 0; J < pnum && I * p[j] <= N; j +) {isp[I * p[j]] = 1; The I-p[j of all prime numbers before I is sifted out if (!) (
        I% p[j])///If I divide a p[j], stop the break;
    }} void Show (bool *p,int N) {for (int i=2;i<=n;i++) {if (!p[i)) printf ("%d", I); } int Main () {int n=100000;//Getprime (n);
GetPrime2 (n);
    Show (Isp,n);
return 0;
 }


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.