-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathConsecutive_Prime_Sum.cpp
More file actions
130 lines (113 loc) · 1.93 KB
/
Consecutive_Prime_Sum.cpp
File metadata and controls
130 lines (113 loc) · 1.93 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
Some prime numbers can be expressed as a sum of other consecutive prime numbers.
For Example
5
=
2
+
3
5=2+3,
17
=
2
+
3
+
5
+
7
17=2+3+5+7,
41
=
2
+
3
+
5
+
7
+
11
+
13
41=2+3+5+7+11+13.
Your task is to find out how many prime numbers which satisfy this property are present in the range
3
3 to
N
N subject to a constraint that summation should always start with number
2
2.
Write code to find out the number of prime numbers that satisfy the above-mentioned property in a given range.
Input Format
The first line of input will contain a single integer
T
T, denoting the number of test cases.
Each test case consists of a single integer
N
N
Output Format
Print the total number of all such prime numbers which are less than or equal to
N
N.
Constraints
1
≤
T
≤
1
0
5
1≤T≤10
5
2
<
N
≤
1
0
5
2<N≤10
5
// ---------------------------SOLUTION--------------------------------------- //
#include <bits/stdc++.h>
using namespace std;
const int lt = 1e5 + 1; // contraints given
vector < int > prime; // no. of prime numbers found in between the limit
vector < bool > isPrime(lt,true); // marks true or false for the nnumbers as prime
int main() {
for(int i=2 ; i*i<=lt; i++){
if(isPrime[i]){
prime.push_back(i);
for(int j=i*i; j<lt; j+=i)
isPrime[j]=false;
}
}
int T;
cin >> T;
// stack for plag
vector < int > ans;
while(T--){ // || while(T != 0)
int N;
cin >> N;
int sum = 5; // 2+3
int i = 2;
int cnt = 0;
while(sum <= N){
if(isPrime[sum])
cnt++;
sum += prime[i];
i++;
}
//cout << cnt << endl;
ans.push_back(cnt);
}
// print the res
for(int i : ans){
cout << i << endl;
}
// while(!st.empty()){
// cout << st.top() << endl;
// st.pop();
// }
// return 0;
}