1 #include<iostream> 2 #include<cstring> 3 using namespace std; 4 const int maxn=1e6+10; 5 6 long long l,r,prime[maxn],vis[maxn],p[maxn],a[maxn]; 7 int tot=0; 8 9 int pre(long long n) 10 { 11 for(int i=2;i<=n;i++) 12 { 13 if(!vis[i]) 14 { 15 vis[i]=i; 16 prime[++tot]=i; 17 } 18 for(int j=1;j<=tot;j++) 19 { 20 if(i*prime[j]>n) 21 { 22 break; 23 } 24 if(prime[j]>vis[i]) 25 { 26 break; 27 } 28 vis[i*prime[j]]=prime[j]; 29 } 30 } 31 } 32 int main() 33 { 34 pre(5e4+10); 35 while(cin>>l>>r) 36 { 37 memset(p,0,sizeof(p)); 38 if(l==1) 39 { 40 l=2; 41 } 42 long long cnt=0,min1,min2,max1,max2,minn=0x3f3f3f3f,maxx=-0x3f3f3f3f; 43 for(int j=1;j<=tot;j++) 44 { 45 for(int i=l/prime[j];i<=r/prime[j];i++) 46 { 47 while(i<=1) 48 { 49 i++; 50 } 51 if(prime[j]*i<l||prime[j]*i>r) 52 { 53 continue; 54 } 55 p[prime[j]*i-l]=1; 56 } 57 } 58 for(int i=l;i<=r;i++) 59 { 60 if(p[i-l]==0) 61 { 62 a[++cnt]=i; 63 } 64 } 65 if(cnt<=1) 66 { 67 //cout<<cnt<<endl; 68 cout<<"There are no adjacent primes."<<endl; 69 continue; 70 } 71 for(int i=1;i<=cnt-1;i++) 72 { 73 if(a[i+1]-a[i]<minn) 74 { 75 minn=a[i+1]-a[i]; 76 min1=a[i+1]; 77 min2=a[i]; 78 } 79 if(a[i+1]-a[i]>maxx) 80 { 81 maxx=a[i+1]-a[i]; 82 max1=a[i+1]; 83 max2=a[i]; 84 } 85 } 86 //cout<<cnt<<endl; 87 cout<<min2<<","<<min1<<" are closest, "<<max2<<","<<max1<<" are most distant."<<endl; 88 } 89 return 0; 90 }