【题目分析】
素数筛+二分
【代码】
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
int top=0,n=1299709;
bool have[1299710];
int pri[1300000],x;
inline void init()
{
memset(have,false,sizeof have);
for (int i=2;i<=1299709;++i)
{
if (!have[i]) pri[++top]=i;
for (int j=1;j<=top&&(pri[j]*i)<=1299709;++j)
{
have[pri[j]*i]=true;
if (i%pri[j]==0) break;
}
}
}
int main()
{
init();
while (scanf("%d",&x)!=EOF&&x)
{
if (!have[x]) {printf("0\n"); continue;}
int l=1,r=top;
while (l<r)
{
int mid=(l+r)/2+1;
if (pri[mid]>x) r=mid-1;
else l=mid;
}
printf("%d\n",pri[l+1]-pri[l]);
}
}