blob: 4f7c84f3db1832dc16e494807acb079929ca7a27 (
plain)
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
|
#include "sieve.hh"
#include <cmath>
#include <cstring>
[[nodiscard]] std::vector<int> sieve1(std::size_t size) {
std::vector<bool> n(size, false);
std::vector<int> r;
for (std::size_t i = 2; i < n.size(); ++i) {
if (n[i] == false) {
r.emplace_back(i);
for (std::size_t j = i + i; j < n.size(); j += i)
n[j] = true;
}
}
return r;
}
[[nodiscard]] std::vector<int> sieve2(std::size_t size) {
std::vector<bool> n(size, false);
std::vector<int> r{2};
for (std::size_t i = 3; i < n.size(); i += 2) {
if (n[i] == false) {
r.emplace_back(i);
for (std::size_t j = i + i; j < n.size(); j += i)
n[j] = true;
}
}
return r;
}
[[nodiscard]] std::vector<int> sieve3(std::size_t size) {
std::vector<bool> n(size, false);
std::vector<int> r{2};
std::size_t s = sqrt(n.size());
for (std::size_t i = 4; i <= s; i += 2)
n[i] = true;
for (std::size_t i = 3; i <= s; i += 2) {
if (n[i] == false) {
r.emplace_back(i);
for (std::size_t j = i * i; j < n.size(); j += i)
n[j] = true;
}
}
s += s % 2 ? 2 : 1;
for (std::size_t i = s; i < n.size(); i += 2)
if (n[i] == false)
r.emplace_back(i);
return r;
}
|