-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathcode_1.cpp
61 lines (56 loc) · 1.27 KB
/
code_1.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
//
// code_1.cpp
// Algorithm
//
// Created by Mohd Shoaib Rayeen on 23/11/18.
// Copyright © 2018 Shoaib Rayeen. All rights reserved.
//
#include <iostream>
#include <vector>
using namespace std;
const int MAX = 10000;
struct Range {
int L, R;
};
int prefix[MAX + 1];
void buildPrefix() {
bool prime[MAX + 1];
memset(prime, true, sizeof(prime));
for (int p = 2; p * p <= MAX; p++) {
if (prime[p] == true) {
for (int i = p * 2; i <= MAX; i += p) {
prime[i] = false;
}
}
}
prefix[0] = prefix[1] = 0;
for (int p = 2; p <= MAX; p++) {
prefix[p] = prefix[p - 1];
if (prime[p]) {
prefix[p]++;
}
}
}
void query(vector<Range> List) {
buildPrefix();
int n = int(List.size());
int L , R;
for ( int i = 0; i < n; i++ ) {
L = List[i].L;
R = List[i].R;
cout << "\nCount for ( " << L << " , " << R << " )\t:\t" << prefix[R] - prefix[L - 1] << "\n";
}
}
int main() {
int n;
cout << "\nEnter Number of Queries\t:\t";
cin >> n;
vector<Range> List(n);
cout << "\nEnter Range (L,R) Form\n";
for (int i = 0; i < n; i++ ) {
cin >> List[i].L;
cin >> List[i].R;
}
query(List);
return 0;
}