forked from mathbunnyru/python-cpp-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsieve.cpp
More file actions
20 lines (17 loc) · 450 Bytes
/
sieve.cpp
File metadata and controls
20 lines (17 loc) · 450 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "sieve.h"
namespace abc {
std::vector<size_t> SieveOfEratosthenes(size_t n) {
std::vector<bool> sieve(n + 1, true);
sieve[0] = sieve[1] = false;
std::vector<size_t> result;
for (size_t p = 2; p <= n; p++) {
if (sieve[p]) {
result.push_back(p);
for (size_t i = 2 * p; i <= n; i += p) {
sieve[i] = false;
}
}
}
return result;
}
} // namespace abc