题目描述:
Output the k-th prime number.
输入:
k≤10000
输出:
The k-th prime number.
样例输入:
10
50
样例输出:
29
229
实现代码1(欧拉筛法):
#include <cstdio>
#include <cstring>
const int MAXN = 900001;
int prime[MAXN];//保存素数
bool vis[MAXN];//初始化
void Prime()
{
int cnt = 0;
memset(vis, 0, sizeof(vis));
for (int i = 2; i< MAXN; i++)
{
if (!vis[i])
prime[cnt++] = i;
for (int j = 0; j < cnt && i * prime[j] < MAXN; j++)
{
vis[i*prime[j]] = 1;
if (i%prime[j] == 0);
}
}
}
int main()
{
int k;
Prime();
while(scanf("%d",&k)!=EOF){
printf("%d\n",prime[k-1]);
}
return 0;
}
实现代码2(埃式筛法):
#include <cstdio>
#include <cstring>
const int maxn=900001;
int prime[maxn],num=0;
bool p[maxn]={0};
void Find_Prime(){
for(int i=2;i<maxn;i++){
if(p[i]==false){
prime[num++]=i;
for(int j=i+i;j<maxn;j+=i){
p[j]=true;
}
}
}
}
int main()
{
int k;
Find_Prime();
while(scanf("%d",&k)!=EOF){
printf("%d\n",prime[k-1]);
}
return 0;
}