| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
打表只需79ms#include<stdio.h>
#include<math.h>
#define M 1299710
int a[M],p[M];
int main()
{
int q,i,j,n,t=0;
q=(int)sqrt(M*1.0);
for(i=2;i<M;i++)a[i]=1;
for(i=2;i<=q;i++)
if(a[i])for(j=i+i;j<M;j+=i)a[j]=0;
for(i=2;i<M;i++){
if(a[i]){
a[i]=0;
p[++t]=i;
}
else a[i]=t;
}
while(scanf("%d",&n),n){
if(!a[n])puts("0");
else printf("%d\n",p[a[n]+1]-p[a[n]]);
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator