[ACM] POJ 2689 Prime Distance (large number of screening ranges)

Source: Internet
Author: User
Tags ranges

Prime Distance
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 12811 Accepted: 3420

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 t Wo primes between the both given numbers) or a line giving the pairs of adjacent primes.

Sample Input

2 1714 17

Sample Output

2,3 is closest, 7,11 is most distant. There is no adjacent primes.

Source

Waterloo Local 1998.10.17


Problem Solving Ideas:

Give an interval [l,r], the range is 1<=l< r<=2147483647, the interval length is not more than 1000000

Find the nearest and farthest two primes (i.e., the smallest and largest number of adjacent differences)

Sieve two times, first sift out 1 to 1000000 prime number, because the 1000000^2 has exceeded the int range, this prime number is sufficient.

function Getprim (); prime[] The first sieve out of the prime number of the total is prime[0]

The second use of the sieved prime number to sieve the prime number between the L,r

function getPrime2 (); Isprime[] Infer whether the number is prime prime2[] what are the prime numbers to sift out, a common possession prime2[0] A

Code:

#include <iostream> #include <string.h> #include <stdio.h> #include <cmath> #include < algorithm>using namespace Std;const int maxn=1e6;int prime[maxn+10];void getprime () {memset (prime,0,sizeof (prime))        ;//A starting prime is set to 0 for a prime number (inverse thinking) for (int i=2;i<=maxn;i++) {if (!prime[i]) prime[++prime[0]]=i;             for (int j=1;j<=prime[0]&&prime[j]<=maxn/i;j++) {Prime[prime[j]*i]=1;//prime[k]=1;k is not a prime number        if (i%prime[j]==0) break;    }}}bool isprime[maxn+10];int prime2[maxn+10];void getPrime2 (int l,int R) {memset (isprime,1,sizeof (IsPrime));    isprime[0]=isprime[1]=0;//This sentence can not add, considering the left interval is 2, plus this sentence, prime 2,3 will be sentenced to composite if (l<2) l=2; for (int i=1;i<=prime[0]&& (long Long) prime[i]*prime[i]<=r;i++) {int s=l/prime[i]+ (l%prime[i]>0)        ,//Calculate the first larger than L and can be prime[i] divisible by the number of prime[i] several times, from here to start sieve if (s==1)//very special, assuming that starting from 1 sieve, then 2 will be screened into non-prime s=2; for (int j=s; (Long Long) J*prime[i]<=r;j++) if ((Long Long) j*prime[i]>=l) Isprime[j*prime[i]-l]=false;    Interval mapping, for example, interval length of 4 interval [4,7], mapped to [0,3], because the topic range 2,147,483,647 array cannot be opened} prime2[0]=0; for (int i=0;i<=r-l;i++) if (isprime[i]) prime2[++prime2[0]]=i+l;}    int main () {getprime ();    int l,r;        while (scanf ("%d%d", &l,&r)!=eof) {getPrime2 (l,r);        if (prime2[0]<2) printf ("There is no adjacent primes.\n");            else {int x1=0,x2=1000000,y1=0,y2=0;                    for (int i=1;i<prime2[0];i++) {if (prime2[i+1]-prime2[i]<x2-x1) {                    X1=prime2[i];                X2=PRIME2[I+1];                    } if (prime2[i+1]-prime2[i]>y2-y1) {y1=prime2[i];                Y2=PRIME2[I+1];        }} printf ("%d,%d is closest,%d,%d is most distant.\n", x1,x2,y1,y2); }} return 0;}



Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

[ACM] POJ 2689 Prime Distance (large number of screening ranges)

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.