#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+100;
const int maxn=1e6+100;
int cnt;
bool vis[N+100];
int prime[N/10];
bool fw[maxn];
void pri()
{
cnt=0;
vis[0]=vis[1]=1;
for(int i=2; i<N; i++)
{
if(!vis[i])
{
prime[cnt++]=i;
//printf("%d ",i);
}
for(int j=0; j<cnt; j++)
{
int x=prime[j];
if(x*i>N)
break;
vis[i*x]=1;
if(i%prime[j]==0)
break;
}
}
}
ll MAX(ll a,ll b)
{
if(a>b)
return a;
return b;
}
int main()
{
pri();
ll l,u;
while(scanf("%lld%lld",&l,&u)!=EOF)
{
memset(fw,0,sizeof(fw));
for(int i=0; i<cnt; i++)
{
ll k1=ceil((l+prime[i]-1)/prime[i]),k2=floor(u/prime[i]);
//printf("***%d %d %d\n",prime[i],k1,k2);
for(int j=MAX(k1,2); j<=k2; j++)
{
fw[j*prime[i]-l]=1;
}
}
if(l==1)
fw[0]=1;
ll minn=maxn,maxx=0;
ll nl,nr,fl,fr;
ll tot=-1;
for(int i=0; i<=u-l; i++)
{
if(!fw[i])
{
if(tot>=0)
{
if(i-tot>maxx)
{
maxx=i-tot;
fl=tot+l;
fr=i+l;
}
if(i-tot<minn)
{
minn=i-tot;
nl=tot+l;
nr=i+l;
}
}
tot=i;
}
}
//printf("tot=%d\n",tot);
if(maxx==0)
{
printf("There are no adjacent primes.\n");
//return 0;
}
else
{
printf("%lld,%lld are closest, %lld,%lld are most distant.\n",nl,nr,fl,fr);
}
}
return 0;
}
08-08
444
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-23
07-23
07-23