-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherat.cc
52 lines (47 loc) · 1.05 KB
/
erat.cc
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
// C++20 implementation of the Sieve of Eratosthenes.
#include <array>
#include <concepts>
#include <iostream>
#include <iterator>
// Mark all the nonprimes for a factor.
template<std::random_access_iterator I, std::integral N>
void mark_sieve (I first, I last, N factor)
{
*first = false;
while (last - first > factor)
{
first += factor;
*first = false;
}
}
template<std::random_access_iterator I, std::integral N>
void sift (I first, N n)
{
I last = first + n;
std::fill (first, last, true);
N i(0);
N index_square(3);
// Factor/value at index i.
N factor(3);
while (index_square < n)
{
// index_square = 2i^2 + 6i + 3
if (first[i])
mark_sieve (first + index_square, last, factor);
++i;
// Index of the first value we want to mark.
index_square += factor;
factor += N(2);
index_square += factor;
}
}
int
main ()
{
constexpr int sz = 30;
std::array<bool, sz> a;
sift (a.begin (), sz);
for (int i = 0; i < sz; ++i)
if (a[i])
std::cout << 2 * i + 3 << "\n";
}