首页 > 代码库 > 【第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小素数 】 打表问题