-
Notifications
You must be signed in to change notification settings - Fork 256
/
primes.cpp
181 lines (154 loc) · 3.88 KB
/
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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include <algorithm>
#include <iostream>
#include <queue>
#include <sstream>
#include <unordered_map>
#include <vector>
#include <libnotify.h>
#ifdef __clang__
static constexpr auto COMPILER = "clang++";
#else
static constexpr auto COMPILER = "g++";
#endif
static const auto UPPER_BOUND = 5'000'000;
static const auto PREFIX = 32'338;
#include <memory_resource>
namespace pmr = std::pmr;
struct Node {
using Map = pmr::unordered_map<char, Node *>;
Map children;
bool terminal = false;
Node(pmr::monotonic_buffer_resource &alloc)
: children(Map::allocator_type{&alloc})
{}
};
class Sieve {
int limit;
std::vector<bool> prime;
Sieve &omit_squares() {
for (auto r = 5; r * r < limit; ++r) {
if (prime[r]) {
for (auto i = r * r; i < limit; i += r * r) {
prime[i] = false;
}
}
}
return *this;
}
void step1(int x, int y) {
const auto n = (4 * x * x) + (y * y);
if (n <= limit && (n % 12 == 1 || n % 12 == 5)) {
prime[n] = !prime[n];
}
}
void step2(int x, int y) {
const auto n = (3 * x * x) + (y * y);
if (n <= limit && n % 12 == 7) {
prime[n] = !prime[n];
}
}
void step3(int x, int y) {
const auto n = (3 * x * x) - (y * y);
if (x > y && n <= limit && n % 12 == 11) {
prime[n] = !prime[n];
}
}
void loop_y(int x) {
for (auto y = 1; y * y < limit; ++y) {
step1(x, y);
step2(x, y);
step3(x, y);
}
}
void loop_x() {
for (auto x = 1; x * x < limit; ++x) {
loop_y(x);
}
}
public:
Sieve(int limit) : limit(limit), prime(limit + 1, false) {}
std::vector<int> to_list() const {
std::vector<int> result({2, 3});
for (auto p = 5; p <= limit; ++p) {
if (prime[p]) {
result.push_back(p);
}
}
return result;
}
Sieve &calc() {
loop_x();
return omit_squares();
}
};
Node* make_node(pmr::monotonic_buffer_resource &alloc)
{
return new(alloc.allocate(sizeof(Node), alignof(Node))) Node(alloc);
}
Node* generate_trie(pmr::monotonic_buffer_resource &alloc, const std::vector<int> &l) {
auto root = make_node(alloc);
for (const auto el : l) {
auto* head = root;
for (const auto ch : std::to_string(el)) {
auto it = head->children.find(ch);
if (it == head->children.end()) {
it = head->children.emplace(ch, make_node(alloc)).first;
}
head = it->second;
}
head->terminal = true;
}
return root;
}
std::vector<int> find(pmr::monotonic_buffer_resource &&alloc, int upper_bound, int prefix) {
const auto primes = Sieve(upper_bound).calc();
const auto &str_prefix = std::to_string(prefix);
auto head = generate_trie(alloc, primes.to_list());
for (const auto ch : str_prefix) {
head = head->children[ch];
}
std::queue<std::pair<Node*, std::string>> queue(
{ std::make_pair(head, str_prefix) }
);
std::vector<int> result;
while (!queue.empty()) {
const auto [top, prefix] = queue.front();
queue.pop();
if (top->terminal) {
result.push_back(std::stoi(prefix));
}
for (const auto &[ch, v] : top->children) {
queue.emplace(v, prefix + ch);
}
}
std::sort(result.begin(), result.end());
return result;
}
std::string to_string(const std::vector<int> &a) {
std::stringstream ss;
ss << "[";
auto first = true;
for (const auto el : a) {
if (!first) {
ss << ", ";
}
ss << el;
first = false;
}
ss << "]";
return ss.str();
}
void verify() {
std::vector<int> left({2, 23, 29});
auto right = find(pmr::monotonic_buffer_resource(), 100, 2);
if (left != right) {
std::cerr << to_string(left) << " != " << to_string(right) << std::endl;
exit(EXIT_FAILURE);
}
}
int main() {
verify();
const auto &results = notifying_invoke(
[&]() { return find(pmr::monotonic_buffer_resource(), UPPER_BOUND, PREFIX); }, "C++/{}", COMPILER);
std::cout << to_string(results) << std::endl;
}