Skip to content

Commit 1d2c5d9

Browse files
Panquesito7github-actions[bot]Keval Kapdee
authored
feat: add Sieve of Eratosthenes (originally by @thechubbypanda) (#2442)
* feat: add Sieve of Eratosthenes Originally created by @thechubbypanda. * updating DIRECTORY.md --------- Co-authored-by: github-actions[bot] <[email protected]> Co-authored-by: Keval Kapdee <[email protected]>
1 parent 0f20cdc commit 1d2c5d9

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@
189189
* [Check Prime](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/math/check_prime.cpp)
190190
* [Complex Numbers](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/math/complex_numbers.cpp)
191191
* [Double Factorial](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/math/double_factorial.cpp)
192+
* [Eratosthenes](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/math/eratosthenes.cpp)
192193
* [Eulers Totient Function](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/math/eulers_totient_function.cpp)
193194
* [Extended Euclid Algorithm](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/math/extended_euclid_algorithm.cpp)
194195
* [Factorial](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/math/factorial.cpp)

math/eratosthenes.cpp

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/**
2+
* @file
3+
* @brief [The Sieve of
4+
* Eratosthenes](https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes)
5+
* @details
6+
* Store an array of booleans where a true value indicates that it's index is
7+
* prime. For all the values in the array starting from 2 which we know is
8+
* prime, we walk the array in multiples of the current outer value setting them
9+
* to not prime. If we remove all multiples of a value as we see it, we'll be
10+
* left with just primes.
11+
*
12+
* Pass "print" as a command line arg to see the generated list of primes
13+
* @author [Keval Kapdee](https://github.com/thechubbypanda)
14+
*/
15+
16+
#include <cassert> /// For assert
17+
#include <chrono> /// For timing the sieve
18+
#include <iostream> /// For IO operations
19+
#include <string> /// For string handling
20+
#include <vector> /// For std::vector
21+
22+
/**
23+
* @namespace math
24+
* @brief Mathematical algorithms
25+
*/
26+
namespace math {
27+
/**
28+
* @brief Performs the sieve
29+
* @param vec Array of bools, all initialised to true, where the number of
30+
* elements is the highest number we wish to check for primeness
31+
* @returns void
32+
*/
33+
void sieve(std::vector<bool> *vec) {
34+
(*vec)[0] = false;
35+
(*vec)[1] = false;
36+
37+
// The sieve sets values to false as they are found not prime
38+
for (uint64_t n = 2; n < vec->size(); n++) {
39+
for (uint64_t multiple = n << 1; multiple < vec->size();
40+
multiple += n) {
41+
(*vec)[multiple] = false;
42+
}
43+
}
44+
}
45+
46+
/**
47+
* @brief Prints all the indexes of true values in the passed std::vector
48+
* @param primes The vector that has been passed through `sieve(...)`
49+
* @returns void
50+
*/
51+
void print_primes(std::vector<bool> const &primes) {
52+
for (uint64_t i = 0; i < primes.size(); i++) {
53+
if (primes[i]) {
54+
std::cout << i << std::endl;
55+
}
56+
}
57+
}
58+
} // namespace math
59+
60+
/**
61+
* @brief Self-tests the sieve function for major inconsistencies
62+
* @returns void
63+
*/
64+
static void test() {
65+
auto primes = std::vector<bool>(10, true);
66+
math::sieve(&primes);
67+
assert(primes[0] == false);
68+
assert(primes[1] == false);
69+
assert(primes[2] == true);
70+
assert(primes[3] == true);
71+
assert(primes[4] == false);
72+
assert(primes[5] == true);
73+
assert(primes[6] == false);
74+
assert(primes[7] == true);
75+
assert(primes[8] == false);
76+
assert(primes[9] == false);
77+
78+
std::cout << "All tests have successfully passed!\n";
79+
}
80+
81+
/**
82+
* @brief Main function
83+
* @param argc commandline argument count
84+
* @param argv commandline array of arguments
85+
* @returns 0 on exit
86+
*/
87+
int main(int argc, char *argv[]) {
88+
test(); // run self-test implementations
89+
90+
// The largest prime we will check for
91+
auto max = 10000;
92+
93+
// Store a boolean for every number which states if that index is prime or
94+
// not
95+
auto primes = std::vector<bool>(max, true);
96+
97+
// Store the algorithm start time
98+
auto start = std::chrono::high_resolution_clock::now();
99+
100+
// Run the sieve
101+
math::sieve(&primes);
102+
103+
// Time difference calculation
104+
auto time = std::chrono::duration_cast<
105+
std::chrono::duration<double, std::ratio<1>>>(
106+
std::chrono::high_resolution_clock::now() - start)
107+
.count();
108+
109+
// Print the primes if we see that "print" was passed as an arg
110+
if (argc > 1 && argv[1] == std::string("print")) {
111+
math::print_primes(primes);
112+
}
113+
114+
// Print the time taken we found earlier
115+
std::cout << "Time taken: " << time << " seconds" << std::endl;
116+
117+
return 0;
118+
}

0 commit comments

Comments
 (0)