-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathprimeSum.cpp
More file actions
29 lines (25 loc) · 769 Bytes
/
primeSum.cpp
File metadata and controls
29 lines (25 loc) · 769 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
class Solution {
public:
vector<int> primesum(int N) {
// Generate isPrime List less equal than N
vector<bool> isPrime(N + 1, true);
isPrime[0] = false;
isPrime[1] = false;
// Sieve of Erastothenes
for(int i = 2; i <= N; i++) {
if (!isPrime[i]) continue;
if (i > N / i) break;
for (int j = i * i; j <= N; j += i) isPrime[j] = false;
}
for(int i = 2; i <= N; ++i) {
if(isPrime[i] && isPrime[N - i]) {
vector<int> ans;
ans.push_back(i);
ans.push_back(N - i);
return ans;
}
}
vector<int> ans;
return ans;
}
};