Prime number Screening (2 times): poj2689 Prime Distance

Source: Internet
Author: User

Prime Distance

Description the branch of mathematics called Number theory is about properties of numbers. One of the areas that's have captured the interest of number theoreticians for thousands of years's the question of Primalit Y. A prime number is a number and this is have has no proper factors (it's only evenly divisible by 1 and itself). The first prime numbers is 2,3,5,7 but they quickly become less frequent. One of the interesting questions is what dense they is in various ranges. Adjacent primes is and numbers that is both primes, but there is no other prime numbers between the adjacent primes. For example, 2,3 is the only adjacent primes that is also adjacent numbers.
Your program was given 2 numbers:l and U (1<=l< u<=2,147,483,647), and you were to find the both adjacent primes C1 and C2 (l<=c1< C2<=u) that's closest (i.e. C2-C1 is the minimum). If There is other pairs that is the same distance apart, use the first pair. You is also to find the adjacent primes D1 and D2 (l<=d1< d2<=u) where D1 and D2 is as distant from each OT Her as possible (again choosing, the first pair if there is a tie).

Input each line of input would contain the positive integers, L and u, with L < U. The difference between L and U would not exceed 1,000,000.

Output for each L and U, the output would either is the statement that there is no adjacent primes (because there is less than primes between the given numbers) or a line giving the both pairs of adjacent primes.

Sample Input

2
14 17

Sample Output

2,3 is closest, 7,11 is most distant.
There is no adjacent primes.
Problem Solving Ideas:

First of all, unlike ordinary prime number screening, because the upper limit of u is very large, only one time from 2-2147483647 of the filter will definitely time out, and do not work, unless the improved algorithm, otherwise you need to change ideas.

Then, you can consider only the number of l-u within the filter, the method is filtered from the 1-sqrt (2147483647) in the prime number of the filter (the prime factor is certainly not more than sqrt (2147483647)), Getprime () function implementation.

Next, the number of l-u intervals is screened with a possible element factor, and the idea of the sieve is the same (but note where the begin is, how thebegin is determined), and all the primes are stored in the array q[].

Finally, for loop once, find the maximum and minimum value of adjacent q[i+1], q[i], output.

The idea is not difficult, the most lame is the data overflow problem.

①for (int i=l;i<=u;i++) when i=u=2147483647, i++ overflow, into the dead loop, so to deal with it.

②long long begin=l%p[i]?l+ (P[i]-l%p[i]): L+l%p[i]; When the begin is determined, there is an addition that can overflow, with a long long p[] and a long long, or you can change to a long Long or unsigned int

Reference code and some comments:

#include 
#include 
#include 
#include #include #include
using namespace std;
const int MAXN=50000+10;
int L,U,K,Q[MAXN];
BOOL prime1[maxn],prime2[1000010];
Long long P[2*MAXN];
The prime number of void Getprime ()                          //1-50000 is filtered
{
    memset (prime1,true,sizeof (prime1));
    prime1[1]=0;
    k=0;
    for (int i=2;i<=maxn;i++) {
        if (Prime1[i]) {
            p[k++]=i;
            for (int j=i+i;j<=maxn;j+=i)
                prime1[j]=false;}}
}
void Solve ()
{
    memset (prime2,true,sizeof (prime2)), if (l==1) l++;                                The problem of prime number screening for 1 is often a pit ...
    ... ..... for (int i=0;imax) {max=q[i+1]-q[i];max1=q[i];max2=q[i+1];}
       Cout<>l>>u) {
    solve ();
   }
    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.