-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathprime.cpp
47 lines (42 loc) · 1.04 KB
/
prime.cpp
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
Given an even number ( greater than 2 ), return two prime numbers whose sum will be equal to given number.
void calculatePrime(bool isPrime[],int num){
for(int i=2;i*i<num;i++){
if(isPrime[i]==true){
for(int j=i*i;j<num;j=j*i){
isPrime[j]=false;
}
}
}
for(int i=1;i<=num;i++){
cout<<isPrime[i]<<" ";
}
cout<<endl;
}
bool checkPrime(bool isPrime[],int i){
if(isPrime[i]==true){
return true;
}
return false;
}
vector<int> Solution::primesum(int a) {
int num=a;
bool isPrime[a+1];//marking all numbers as prime
for(int i=1;i<=num;i++){
isPrime[i]=true;
}
isPrime[0]=false;
isPrime[1]=false;
isPrime[2]=true;
calculatePrime(isPrime,a);
int x = a-2;
for(int i=2;i<a;i++){
if(checkPrime(isPrime,i)){
if(checkPrime(isPrime,a-i)){
vector<int>v;
v.push_back(i);
v.push_back(a-i);
return v;
}
}
}
}