#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int num,n,prime[1000000];
bool v[20000000];
void prepare()
{
num=0;
for(int i=2;i<1500000;i++)
{
if(!v[i]) prime[num++]=i;
for(int j=0;j<num&&i*prime[j]<1500000;j++)
{
v[i*prime[j]]=1;
if(!(i % prime[j])) break;
}
}
}
int main()
{
memset(v,0,sizeof(v));
prepare();
while(scanf("%d",&n)==1&&n)
{
//cout<<n<<endl;
int top1=lower_bound(prime,prime+num,n)-prime-1;
int top2=upper_bound(prime,prime+num,n)-prime;
if(!v[n]) cout<<0<<endl;
else
cout<<prime[top2]-prime[top1]<<endl;
}
return 0;
}
UVA 1644(p343)----Prime Gap
最新推荐文章于 2022-10-09 23:28:27 发布