-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
/
sieve_of_eratosthenes.cpp
122 lines (111 loc) · 4.02 KB
/
sieve_of_eratosthenes.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
/**
* @file
* @brief Prime Numbers using [Sieve of
* Eratosthenes](https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes)
* @details
* Sieve of Eratosthenes is an algorithm that finds all the primes
* between 2 and N.
*
* Time Complexity : \f$O(N \cdot\log \log N)\f$
* <br/>Space Complexity : \f$O(N)\f$
*
* @see primes_up_to_billion.cpp prime_numbers.cpp
*/
#include <cstdint>
#include <cassert> /// for assert
#include <iostream> /// for IO operations
#include <vector> /// for std::vector
/**
* @namespace math
* @brief Mathematical algorithms
*/
namespace math {
/**
* @namespace sieve_of_eratosthenes
* @brief Functions for finding Prime Numbers using Sieve of Eratosthenes
*/
namespace sieve_of_eratosthenes {
/**
* @brief Function to sieve out the primes
* @details
* This function finds all the primes between 2 and N using the Sieve of
* Eratosthenes algorithm. It starts by assuming all numbers (except zero and
* one) are prime and then iteratively marks the multiples of each prime as
* non-prime.
*
* Contains a common optimization to start eliminating multiples of
* a prime p starting from p * p since all of the lower multiples
* have been already eliminated.
* @param N number till which primes are to be found
* @return is_prime a vector of `N + 1` booleans identifying if `i`^th number is
* a prime or not
*/
std::vector<bool> sieve(uint32_t N) {
std::vector<bool> is_prime(N + 1, true); // Initialize all as prime numbers
is_prime[0] = is_prime[1] = false; // 0 and 1 are not prime numbers
for (uint32_t i = 2; i * i <= N; i++) {
if (is_prime[i]) {
for (uint32_t j = i * i; j <= N; j += i) {
is_prime[j] = false;
}
}
}
return is_prime;
}
/**
* @brief Function to print the prime numbers
* @param N number till which primes are to be found
* @param is_prime a vector of `N + 1` booleans identifying if `i`^th number is
* a prime or not
*/
void print(uint32_t N, const std::vector<bool> &is_prime) {
for (uint32_t i = 2; i <= N; i++) {
if (is_prime[i]) {
std::cout << i << ' ';
}
}
std::cout << std::endl;
}
} // namespace sieve_of_eratosthenes
} // namespace math
/**
* @brief Self-test implementations
* @return void
*/
static void tests() {
std::vector<bool> is_prime_1 =
math::sieve_of_eratosthenes::sieve(static_cast<uint32_t>(10));
std::vector<bool> is_prime_2 =
math::sieve_of_eratosthenes::sieve(static_cast<uint32_t>(20));
std::vector<bool> is_prime_3 =
math::sieve_of_eratosthenes::sieve(static_cast<uint32_t>(100));
std::vector<bool> expected_1{false, false, true, true, false, true,
false, true, false, false, false};
assert(is_prime_1 == expected_1);
std::vector<bool> expected_2{false, false, true, true, false, true,
false, true, false, false, false, true,
false, true, false, false, false, true,
false, true, false};
assert(is_prime_2 == expected_2);
std::vector<bool> expected_3{
false, false, true, true, false, true, false, true, false, false,
false, true, false, true, false, false, false, true, false, true,
false, false, false, true, false, false, false, false, false, true,
false, true, false, false, false, false, false, true, false, false,
false, true, false, true, false, false, false, true, false, false,
false, false, false, true, false, false, false, false, false, true,
false, true, false, false, false, false, false, true, false, false,
false, true, false, true, false, false, false, false, false, true,
false, false, false, true, false, false, false, false, false, true,
false, false, false, false, false, false, false, true, false, false,
false};
assert(is_prime_3 == expected_3);
}
/**
* @brief Main function
* @returns 0 on exit
*/
int main() {
tests();
return 0;
}