-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path007.cc
63 lines (53 loc) · 1.27 KB
/
007.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <cmath>
#include <math.h>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
/*
By listing the first six prime numbers: 2, 3, 5, 7 and 13 , we can see that the 6th prime is 13.
What is the Nth prime number?
Input Format
First line contains T that denotes the number of test cases. This is followed by T lines, each containing an integer, N.
Output Format
Print the required answer for each test case.
Constraints
1 <= T <= 10^3
1 <= N <= 10^4
Sample Input
2
3
6
Sample Output
5
13
-------------
To solve, I used trial division with memoization.
*/
bool isValuePrime(int testValue, vector<int> knownPrimes) {
int i = 0;
int max = sqrt(testValue);
for(int i = 0; knownPrimes[i] <= max; i++) {
if (testValue % knownPrimes[i] == 0) {
return false;
}
}
return true;
}
int main() {
int testCases;
int testValue;
vector<int> primes;
primes.push_back(2);
cin >> testCases;
for (int testCase = 0; testCase < testCases ; testCase++ ) {
cin >> testValue;
for(int potentialPrime = primes.back()+1; primes.size() <= testValue; potentialPrime++) {
if (isValuePrime(potentialPrime, primes)) {
primes.push_back(potentialPrime);
}
}
cout << primes[testValue-1] << endl;
}
}