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 prime S 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 th An primes between the both given numbers) or a line giving the both 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.
Code
#include <stdio.h>#include<string.h>#include<math.h>#include<algorithm>using namespacestd;Const intmr=1e6,mx=5e4+Ten; intL,R,CNT,PRI[MX];BOOLISPR[MX],F[MR];voidYcl () {memset (ISPR,1,sizeof(ISPR)); ispr[1]=0; intM=SQRT (mx+0.5); for(intI=2; i<=m;++i)if(Ispr[i]) { for(intJ=i+i;j<=mx;j+=i) ispr[j]=0; } for(intI=2; i<=mx;++i) {if(Ispr[i]) pri[++cnt]=i; } }intMain () {YCL (); while(SCANF ("%d%d", &l,&r)! =EOF) { if(l==1) l=2; Memset (F,0,sizeof(f)); for(intI=1; i<=cnt;++i) {intA= (l1)/pri[i]+1; intb=r/Pri[i]; for(intJ=A;J<=B;++J)if(j>1) f[pri[i]*j-l]=1; } intp=-1, mx=-1, mn=1e9,xx,yx,xn,yn; for(intI=0; i<=r-l;++i) {if(!F[i]) { if(p!=-1) { if(mx<i-p) {MX=i-p; XX=p+l,yx=i+l; } if(mn>i-p) {mn=i-p; Xn=p+l,yn=i+l; }} P=i; } } if(mx==-1) printf ("there is no adjacent primes.\n"); Elseprintf"%d,%d is closest,%d,%d is most distant.\n", Xn,yn,xx,yx); } return 0;}
Prime Distance (POJ 2689)