-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy path7.cpp
55 lines (49 loc) · 1.13 KB
/
7.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
# include <iostream>
using std::cin;
using std::cout;
using std::endl;
void Generate (int List[], int N) {
for (int i = 0; i < N; i++) List[i] = i;
List[0] = List[1];
}
void Mark (int List[], int N, int &prime) {
int i = prime * prime;
if (i < 0) {
for (i = prime; i < N; i += prime)
if (i == prime) continue;
else if (List[i] % prime == 0) List[i] = 0;
}
else {
for (; i < N; i += prime)
if (List[i] % prime == 0) List[i] = 0;
}
}
int FindNextPrime (int List[], int N, int &prime) {
for (int i = (prime + 1); i < N; i++)
if (List[i] > prime && List[i] != 0)
return List[i];
return -1;
}
void PrintPrimes (int &L, int &U) {
int *List = new int[U + 1];
Generate (List, (U + 1));
int prime = List[2];
do {
Mark (List, (U + 1), prime);
prime = FindNextPrime (List, (U + 1), prime);
}while (prime > 0);
List[0] = List[1] = 0;
for (int i = L; i <= U; i++)
if (List[i] != 0) cout << List[i] << " ";
delete []List;
}
int main () {
int T, x, y;
cin >> T;
for (int i = 0; i < T; i++) {
cin >> x >> y;
PrintPrimes (x, y);
cout << endl;
}
return 0;
}