首页 > 代码库 > 【第k小素数 】 打表问题
【第k小素数 】 打表问题
Prime Number
TimeLimit: 1 Second MemoryLimit: 32 Megabyte
Totalsubmit: 399 Accepted: 88
Description
We know that the number of prime numbers is countless. Now we want to know the kth small prime number.Input
Input contains multiple test cases. Each test case contains an integer k.(1<=k<=10^5)Output
Print the kth small prime number.Sample Input
1
2
4
Sample Output
2
3
7
#include <iostream>#include <stdio.h>#include <math.h>#include <string.h>#define maxn 10000000using namespace std;int pre[maxn+10];bool p[maxn+10];int num;void fun(){ int limt=sqrt(maxn+0.0); int i,j; num=0; pre[num++]=2; memset(p,0,sizeof(p)); for(i=4;i<=maxn;i+=2)p[i]=1; for(i=3;i<=limt;i+=2) { if(p[i])continue; pre[num++]=i; for(j=i*i;j<=maxn;j+=i)p[j]=1; } for(;i<=maxn;i++)if(!p[i])pre[num++]=i;}int main(){ //freopen("in.txt","r",stdin); fun(); int n; while(cin >> n) { cout << pre[n-1] << endl; } return 0;}
【第k小素数 】 打表问题
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。