UVa1644 - Prime Gap

来源:互联网 发布:linux shell脚本减法 编辑:程序博客网 时间:2024/06/04 18:39

#include<iostream>using namespace std;const unsigned maxn = 1300010;int p[maxn], n;bool np[maxn] = {true, true};int main(){    ios::sync_with_stdio(false);    for(int i=2; i<maxn; i++){        if(!np[i])            p[n++]=i;        for(int j=0;j<n&&i*p[j]<maxn;j++){            np[i*p[j]]=1;            if(!(i%p[j])) break;        }    }        while(cin>>n&&n){            if(!np[n]){                cout<<"0"<<endl;                continue;            }            int i=n,j=n;            while(np[i]) i--;            while(np[j]) j++;            cout<<j-i<<endl;        }    return 0;}
素数怎么求的,明天再看看


0 0
原创粉丝点击