Hoj question dual screening method

Source: Internet
Author: User

Question:
Returns an interval of <= 1000 000; returns the smallest and largest difference between the adjacent prime numbers of prime numbers;
Analysis:
Double screening, where the number of intervals may be large, so it is obviously not feasible to use Trial Division;
First, the prime number from 1 to 47000 is filtered out for the first time.
Determine whether the number in the range from L to U is a prime number;
Because the range is 1 <= L <u <= 2,147,483,647, while the U-L <= 1000 000; so we can set a 1000000
Array within. The res [I-l] value is 0 or 1 to indicate whether it is a prime number;
CoreAlgorithmIs the second screening, where J = begin * prime [I] indicates the prime [I] in the range A and B
The first multiple; filtering in turn;
Heavy screening methodProgramAs follows:
Memset (Prime, false, sizeof (PRIME ));
TOT = 0;
For (INT I = 2; I <X; I ++)
{
If (! Prime [I])
Isprime [tot ++] = I;
For (Int J = I + I; j <X; j = J + I)
Prime [J] = true;
}

The procedure of double screening is as follows:
Memset (Res, false, sizeof (RES ));
If (M = 1)
M = 2;
For (INT I = 0; I <tot & isprime [I] * isprime [I] <= N; I ++)
{
Long long temp = (M/isprime [I]) + (M % isprime [I]> 0 );
If (temp = 1)
Temp = 2;
For (long J = temp * isprime [I]; j <= N; j + = isprime [I])
Res [J-M] = true;
}

# include
# include
# include
using namespace STD;
# define x 50000
int tot;
bool prime [50000];
int isprime [10000];
bool res [1000010];
void make_prime ()
{< br> memset (Prime, false, sizeof (PRIME);
tot = 0;
for (INT I = 2; I {< br> If (! Prime [I])
isprime [tot ++] = I;
for (Int J = I + I; j prime [J] = true;
}< BR >}< br> int main ()
{< br> freopen ("sum. in "," r ", stdin);
freopen (" sum. out "," W ", stdout);
make_prime ();
long n, m;
while (CIN> m> N)
{< br> memset (Res, false, sizeof (RES);
int CNT = 0;
If (M = 1)
m = 2;
for (INT I = 0; I {< br> long temp = (M/isprime [I]) + (M % is Prime [I]> 0);
If (temp = 1)
temp = 2;
for (long J = temp * isprime [I]; j <= N; j + = isprime [I])
res [J-M] = true;
}< br> for (INT I = 0; I <= N-m; I ++)
If (! Res [I])
CNT ++;
cout }< br> 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.