-
Notifications
You must be signed in to change notification settings - Fork 353
/
Copy pathsum_of_two_primes.cpp
80 lines (74 loc) · 1.19 KB
/
sum_of_two_primes.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
Sum of Two Primes
Write a program to find if a given number N can be expressed as a sum of prime numbers.
Input
5
20
12
23
34
16
Output
yes
yes
no
yes
yes
*/
#include<iostream>
#include<cmath>
using namespace std;
bool isPrime(int n)
{
if(n==1)
return false;
else{
for(int i=2;i<=(int)sqrt(n);i++)
{
if(n%i == 0)
return false;
}
return true;
}
}
int main(int argc, char const *argv[])
{
int t,n,count=0,flag=0;
cin>>t;
while (t--)
{
count = 0;
flag = 0;
cin>>n;
int primelist[n];
for(int i=2;i<=n;i++)
{
if(isPrime(i))
{
primelist[count]=i;
count++;
}
}
for(int i=0;i<count-1;i++)
{
for(int j=i+1;j<count;j++)
{
if(primelist[i]+primelist[j] == n)
{
flag = 1;
break;
}
}
if(flag)
break;
}
if (flag)
{
cout<<"yes";
}
else{
cout<<"no";
}
}
return 0;
}