UVA1644 - Prime Gap

来源:互联网 发布:一本书学会做数据分析 编辑:程序博客网 时间:2024/06/03 16:48

筛选法

#include <stdio.h>typedef unsigned long long llu;const llu M=1299710;const llu N=100000+10;int cnt=0;int a[M];int b[N];void sar(){    llu temp;    a[0]=a[1]=1;    for(llu i =2; i<M;i++){        if(a[i]==0){            b[cnt++]=i;            temp=i*i;            while(temp<M) { a[temp]=1 ;temp+=i;}        }    }}int main(){    sar();    int x;    while(scanf("%d",&x)&&x){        for(int i =0;i<100000; i++){            if(b[i]==x){                printf("0\n"); break;            }            else if(b[i]>x){                printf("%d\n",b[i]-b[i-1]); break;            }        }    }    return 0;}


0 0
原创粉丝点击