-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPrime Sum.cpp
More file actions
51 lines (33 loc) · 949 Bytes
/
Copy pathPrime Sum.cpp
File metadata and controls
51 lines (33 loc) · 949 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
40
41
42
43
44
45
46
47
48
49
50
51
Given an even number ( greater than 2 ), return two prime numbers whose sum will be equal to given number.
NOTE A solution will always exist. read Goldbach’s conjecture
Example:
Input : 4
Output: 2 + 2 = 4
If there are more than one solutions possible, return the lexicographically smaller solution.
If [a, b] is one solution with a <= b,
and [c,d] is another solution with c <= d, then
[a, b] < [c, d]
If a < c OR a==c AND b < d.
vector<int> Solution::primesum(int n) {
vector<int> ret;
vector<bool> seive(n+1, true);
seive[0]=false;
seive[1]=false;
for(int i=2;i*i<=n;i++){
int tmp=i*2;
if(seive[i]){
while(tmp<=n){
seive[tmp]=false;
tmp+=i;
}
}
}
for(int i=0;i<n;i++){
if(seive[i] && seive[n-i]){
ret.push_back(i);
ret.push_back(n-i);
break;
}
}
return ret;
}