-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrimeSum.cpp
More file actions
43 lines (41 loc) · 888 Bytes
/
PrimeSum.cpp
File metadata and controls
43 lines (41 loc) · 888 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
/*
QUestion
Prime Sum
Asked in:
Epic systems
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*/
bool* foo(int n)
{
bool *prime=new bool[n+1];
for(int i=0;i<n;i++)
prime[i]=1;
prime[1]=false;
for(int i=2;i*i<=n;i++)
{
if(prime[i])
{
for(int j=i*i;j<=n;j+=i){
prime[j]=false;}
}
}
return prime;
}
vector<int> Solution::primesum(int A) {
bool* prime=foo(A);
vector<int >ans;
int i=2;
//cout<<prime[3]<<" ";
while(1)
{
//cout<<i<<" "<<A-i<<" "<<prime[i]<<" "<<prime[A-i]<<endl;
if(prime[i]&&prime[A-i])
{
ans.push_back(i);
ans.push_back(A-i);
return ans;
}
i++;
}
return ans;
}