forked from Mohammed-Shoaib/Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathB0025.cpp
More file actions
39 lines (34 loc) · 756 Bytes
/
B0025.cpp
File metadata and controls
39 lines (34 loc) · 756 Bytes
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
// Problem Code: PRB01
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void sieveOfEratosthenes(vector<int> &primes){
int i, j, size = 100000;
vector<bool> isPrime(size+1, true);
for(i=2 ; i*i<=size ; i++)
if(isPrime[i]){
primes.push_back(i);
for(j=2*i ; j<=size ; j+=i)
isPrime[j] = false;
}
for(; i<=size ; i++)
if(isPrime[i])
primes.push_back(i);
}
string primalityTest(int N, vector<int> &primes){
bool isPrime = (find(primes.begin(), primes.end(), N) != primes.end());
return (isPrime) ? "yes" : "no";
}
int main()
{
int T, N;
vector<int> primes;
sieveOfEratosthenes(primes);
cin >> T;
for(int i=1 ; i<=T ; i++){
cin >> N;
cout << primalityTest(N, primes) << endl;
}
return 0;
}