Prime Distance
| Time Limit: 1000MS |
|
Memory Limit: 65536K |
| Total Submissions: 17021 |
|
Accepted: 4536 |
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.17personally, the essence of the linear sieve method is that the product of a prime number larger than a composite and the composite can be multiplied by a larger composite and smaller prime numbers. Well
1#include <iostream>2#include <cstring>3#include <cstdio>4 #defineN 500005 #defineLen 10000006 #defineINF 0x7fffffff7 using namespacestd;8typedefLong LongLL;9 BOOLisprime[n+5];Ten LL prime[n],cnt; One BOOLres[len+5]; A voidinit () { - LL i,j; -Cnt=0; thememset (IsPrime,true,sizeof(IsPrime)); - for(i=2; i<=n;i++){ - if(Isprime[i]) prime[cnt++]=i; - for(j=0; j<cnt && i*prime[j]<=n;j++){ +isprime[i*prime[j]]=false; - if(i%prime[j]==0) Break; + } A } at } - intMain () { -Freopen ("prime.in","R", stdin); -Freopen ("Prime.out","W", stdout); - LL l,u,i,j,k,min,max,s,t; - init (); in while(SCANF ("%lld%lld", &l,&u)! =EOF) { -memset (Res,0,sizeof(res)); to for(i=0; i<cnt;i++){ +S= (l1)/prime[i]+1; -t=u/Prime[i]; the for(j=s;j<=t;j++) * if(j>1) $res[j*prime[i]-l]=true;Panax Notoginseng } -k=-1, min=inf,max=-1; the LL dis,m1,m2; + for(i=0; i<=u-l;i++){ A if(!Res[i]) { the if(k!=-1){ +dis=i-K; - if(dis>max) { $max=dis; $m1=i; - } - if(dis<min) { themin=dis; -M2=i;Wuyi } the } - if(i+l!=1)//Note that when you're 1, special judgment 1 is not prime. Wuk=i; - } About } $ if(max==-1) -printf"there is no adjacent primes.\n"); - Else{ -printf"%lld,%lld is closest,%lld,%lld is most distant.\n", m2- Amin+l,m2+l,m1-max+l,m1+L); + } the } - return 0; $}Meow ~
POJ-2689 Prime Distance (linear sieve method)