TOJ 3105.A Way To Find Primes(埃式筛法)

题目链接:http://acm.tju.edu.cn/toj/showp3105.html



3105.    A Way To Find Primes
Time Limit: 1.0 Seconds    Memory Limit: 65536K
Total Runs: 1481    Accepted Runs: 778



Description

Given a integer p > 1, if p can be devided only by 1 and itself, we say that p is a prime number. For example, 31 can only be devided by 1 and 31, so 31 is a prime number; 12 can be devided by six numbers: 1, 2, 3, 4, 6, 12, so 12 is not a prime number. The smallest ten prime number is 2, 3, 5, 7, 11, 13, 17, 19, 23, 29.

Eratosthenes was a Greek mathematician, astronomer, and geographer. He invented a method for finding prime numbers that is still used today. This method is called Eratosthenes'Sieve. A sieve has holes in it and is used to filter out the juice. Eratosthenes's sieve filters out numbers to find the prime numbers.

Eratosthenes's sieve can be used as follows to find all prime numbers smaller than or equal to a given number n:
Step1 : Put all integers between 2 and n (include 2 and n) on Eratosthenes's sieve.
Step2 : Sellect the smallest number on the sieve, suppose it is m, then m must be a prime.
Step3 : Filter out all the integers which can be devided by m from Eratosthenes's sieve.
Step4 : If m * m ≤ n, go to Step 2.
Step5 : Integers remains on the sieve are all prime numbers.

For example, if we want to find all primes between 2 and 20 using Eratosthenes's sieve,Put all integers between 2 and 20 on Eratosthenes's sieve.

Sieve     : 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20
Prime bin :

Sellect the smallest number, it's 2, so it is a prime. Then filter out all integers can be devided by 2:

Sieve     : 3, 5, 7, 9, 11, 13, 15, 17, 19
Prime bin : 2

Sellect the smallest number, it's 3, so it is also a prime. Then filter out all integers can be devided by 2:

Sieve     : 5, 7, 11, 13, 17, 19
Prime bin : 2, 3

Sellect the smallest number, it's 5, so it is also a prime. Then filter out all integers can be devided by 5:

Sieve     : 7, 11, 13, 17, 19
Prime bin : 2, 3, 5

Because 5 * 5 = 25 > 20, so all numbers remains on Eratosthenes's sieve are prime, put all of them into prime bin:

Sieve     :
Prime bin : 2, 3, 5, 7, 11, 13, 17, 19
Now the task is, given a number k, output the k-th smallest prime number. (You may use any way you want to solve this problem as it is correct)

Input

The first line is an integer n, the number of test cases, n cases follows. For Each test case has an single integer k (k > 1).

Output

A single integer per line, the k-th prime number. It is guaranted that the correct answer is smaller than 100000.

Sample Input

4
1
10
100
1000

Sample Output

2
29
541
7919

Hint

If you want to use an large arrays, Please define them as global variables. For example, if I want to use an array named arr[]:

#include ...
...
...

int arr[1000000];

int main () {
    ......
}



Source: 
Submit   List    Runs   Forum   Statistics


水题

#include <stdio.h>
#include <string.h>
bool isPrime[100001];
int prime[10000];
void Prime(int n){
	memset(isPrime,true,sizeof(isPrime));
	int num=0;
	isPrime[0]=isPrime[1]=false;
	for(int i=2;i<=n;i++){
		if(isPrime[i]){
			prime[++num]=i;
			for(int j=2*i;j<=n;j+=i)
				isPrime[j]=false;
			}
	}
}
int main(){
	int n,num;
	scanf("%d",&n);
	Prime(100000);
	while(n--){
		scanf("%d",&num);
		printf("%d\n",prime[num]);
	}
} 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值