forked from Mohammed-Shoaib/Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathE0019.cpp
More file actions
39 lines (33 loc) · 735 Bytes
/
E0019.cpp
File metadata and controls
39 lines (33 loc) · 735 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: POTATOES
#include <iostream>
#include <vector>
using namespace std;
void sieveOfEratosthenes(vector<int> &primes){
int i, j, size = 2003;
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);
}
int farmerFeb(int x, int y, vector<int> &primes){
vector<int>::iterator it = lower_bound(primes.begin(), primes.end(), x+y+1);
return *it-(x+y);
}
int main()
{
int T, x, y;
vector<int> primes;
sieveOfEratosthenes(primes);
cin >> T;
for(int i=1 ; i<=T ; i++){
cin >> x >> y;
cout << farmerFeb(x, y, primes) << endl;
}
return 0;
}