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.
This commit is contained in:
Filip Hlasek 2020-07-23 04:50:38 -07:00 committed by GitHub
parent d58954523f
commit 42e1246ffc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,58 +1,72 @@
/** /**
* @file * @file
* @brief Get list of prime numbers using Sieve of Eratosthenes * @brief Get list of prime numbers using Sieve of Eratosthenes
* Sieve of Eratosthenes is an algorithm to find the primes * @details
* that is between 2 to N (as defined in main). * Sieve of Eratosthenes is an algorithm that finds all the primes
* between 2 and N.
* *
* Time Complexity : \f$O(N \cdot\log N)\f$ * Time Complexity : \f$O(N \cdot\log \log N)\f$
* <br/>Space Complexity : \f$O(N)\f$ * <br/>Space Complexity : \f$O(N)\f$
* *
* @see primes_up_to_billion.cpp prime_numbers.cpp * @see primes_up_to_billion.cpp prime_numbers.cpp
*/ */
#include <iostream> // for io operations #include <cassert>
#include <iostream>
#include <vector>
/** /**
* This is the function that finds the primes and eliminates * This is the function that finds the primes and eliminates the multiples.
* the multiples. * 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 of primes to check * @param N number of primes to check
* @param [out] isprime a boolean array of size `N` identifying if `i`^th number is prime or not * @return is_prime a vector of `N + 1` booleans identifying if `i`^th number is a prime or not
*/ */
void sieve(uint32_t N, bool *isprime) { std::vector<bool> sieve(uint32_t N) {
isprime[0] = true; std::vector<bool> is_prime(N + 1, true);
isprime[1] = true; is_prime[0] = is_prime[1] = false;
for (uint32_t i = 2; i * i <= N; i++) { for (uint32_t i = 2; i * i <= N; i++) {
if (!isprime[i]) { if (is_prime[i]) {
for (uint32_t j = (i << 1); j <= N; j = j + i) { for (uint32_t j = i * i; j <= N; j += i) {
isprime[j] = true; is_prime[j] = false;
} }
} }
} }
return is_prime;
} }
/** /**
* This function prints out the primes to STDOUT * This function prints out the primes to STDOUT
* @param N number of primes to check * @param N number of primes to check
* @param [in] isprime a boolean array of size `N` identifying if `i`^th number is prime or not * @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 bool *isprime) { void print(uint32_t N, const std::vector<bool> &is_prime) {
for (uint32_t i = 2; i <= N; i++) { for (uint32_t i = 2; i <= N; i++) {
if (!isprime[i]) { if (is_prime[i]) {
std::cout << i << ' '; std::cout << i << ' ';
} }
} }
std::cout << std::endl; std::cout << std::endl;
} }
/**
* Test implementations
*/
void tests() {
// 0 1 2 3 4 5 6 7 8 9 10
std::vector<bool> ans{false, false, true, true, false, true, false, true, false, false, false};
assert(sieve(10) == ans);
}
/** /**
* Main function * Main function
*/ */
int main() { int main() {
uint32_t N = 100; tests();
bool *isprime = new bool[N];
sieve(N, isprime);
print(N, isprime);
delete[] isprime;
uint32_t N = 100;
std::vector<bool> is_prime = sieve(N);
print(N, is_prime);
return 0; return 0;
} }