Skip to content

Commit 42e1246

Browse files
authored
fix, test: Refactor of sieve_of_eratosthenes (#969)
* fix, test: Refactor of sieve_of_eratosthenes * Add missing include. * Modernize the vector initialization. * Add @details for the documentation.
1 parent d589545 commit 42e1246

File tree

1 file changed

+35
-21
lines changed

1 file changed

+35
-21
lines changed

math/sieve_of_eratosthenes.cpp

+35-21
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,72 @@
11
/**
22
* @file
33
* @brief Get list of prime numbers using Sieve of Eratosthenes
4-
* Sieve of Eratosthenes is an algorithm to find the primes
5-
* that is between 2 to N (as defined in main).
4+
* @details
5+
* Sieve of Eratosthenes is an algorithm that finds all the primes
6+
* between 2 and N.
67
*
7-
* Time Complexity : \f$O(N \cdot\log N)\f$
8+
* Time Complexity : \f$O(N \cdot\log \log N)\f$
89
* <br/>Space Complexity : \f$O(N)\f$
910
*
1011
* @see primes_up_to_billion.cpp prime_numbers.cpp
1112
*/
1213

13-
#include <iostream> // for io operations
14+
#include <cassert>
15+
#include <iostream>
16+
#include <vector>
1417

1518
/**
16-
* This is the function that finds the primes and eliminates
17-
* the multiples.
19+
* This is the function that finds the primes and eliminates the multiples.
20+
* Contains a common optimization to start eliminating multiples of
21+
* a prime p starting from p * p since all of the lower multiples
22+
* have been already eliminated.
1823
* @param N number of primes to check
19-
* @param [out] isprime a boolean array of size `N` identifying if `i`^th number is prime or not
24+
* @return is_prime a vector of `N + 1` booleans identifying if `i`^th number is a prime or not
2025
*/
21-
void sieve(uint32_t N, bool *isprime) {
22-
isprime[0] = true;
23-
isprime[1] = true;
26+
std::vector<bool> sieve(uint32_t N) {
27+
std::vector<bool> is_prime(N + 1, true);
28+
is_prime[0] = is_prime[1] = false;
2429
for (uint32_t i = 2; i * i <= N; i++) {
25-
if (!isprime[i]) {
26-
for (uint32_t j = (i << 1); j <= N; j = j + i) {
27-
isprime[j] = true;
30+
if (is_prime[i]) {
31+
for (uint32_t j = i * i; j <= N; j += i) {
32+
is_prime[j] = false;
2833
}
2934
}
3035
}
36+
return is_prime;
3137
}
3238

3339
/**
3440
* This function prints out the primes to STDOUT
3541
* @param N number of primes to check
36-
* @param [in] isprime a boolean array of size `N` identifying if `i`^th number is prime or not
42+
* @param is_prime a vector of `N + 1` booleans identifying if `i`^th number is a prime or not
3743
*/
38-
void print(uint32_t N, const bool *isprime) {
44+
void print(uint32_t N, const std::vector<bool> &is_prime) {
3945
for (uint32_t i = 2; i <= N; i++) {
40-
if (!isprime[i]) {
46+
if (is_prime[i]) {
4147
std::cout << i << ' ';
4248
}
4349
}
4450
std::cout << std::endl;
4551
}
4652

53+
/**
54+
* Test implementations
55+
*/
56+
void tests() {
57+
// 0 1 2 3 4 5 6 7 8 9 10
58+
std::vector<bool> ans{false, false, true, true, false, true, false, true, false, false, false};
59+
assert(sieve(10) == ans);
60+
}
61+
4762
/**
4863
* Main function
4964
*/
5065
int main() {
51-
uint32_t N = 100;
52-
bool *isprime = new bool[N];
53-
sieve(N, isprime);
54-
print(N, isprime);
55-
delete[] isprime;
66+
tests();
5667

68+
uint32_t N = 100;
69+
std::vector<bool> is_prime = sieve(N);
70+
print(N, is_prime);
5771
return 0;
5872
}

0 commit comments

Comments
 (0)