码迷,mamicode.com
首页 > 其他好文 > 详细

【第k小素数 】 打表问题

时间:2014-10-09 23:06:21      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   io   os   for   sp   div   

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 10000000
using 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小素数 】 打表问题

标签:des   style   blog   color   io   os   for   sp   div   

原文地址:http://www.cnblogs.com/balfish/p/4014576.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!