-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
/
eratosthenes.cpp
118 lines (103 loc) · 3.42 KB
/
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
/**
* @file
* @brief [The Sieve of
* Eratosthenes](https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes)
* @details
* Store an array of booleans where a true value indicates that it's index is
* prime. For all the values in the array starting from 2 which we know is
* prime, we walk the array in multiples of the current outer value setting them
* to not prime. If we remove all multiples of a value as we see it, we'll be
* left with just primes.
*
* Pass "print" as a command line arg to see the generated list of primes
* @author [Keval Kapdee](https://github.com/thechubbypanda)
*/
#include <cassert> /// For assert
#include <chrono> /// For timing the sieve
#include <iostream> /// For IO operations
#include <string> /// For string handling
#include <vector> /// For std::vector
/**
* @namespace math
* @brief Mathematical algorithms
*/
namespace math {
/**
* @brief Performs the sieve
* @param vec Array of bools, all initialised to true, where the number of
* elements is the highest number we wish to check for primeness
* @returns void
*/
void sieve(std::vector<bool> *vec) {
(*vec)[0] = false;
(*vec)[1] = false;
// The sieve sets values to false as they are found not prime
for (uint64_t n = 2; n < vec->size(); n++) {
for (uint64_t multiple = n << 1; multiple < vec->size();
multiple += n) {
(*vec)[multiple] = false;
}
}
}
/**
* @brief Prints all the indexes of true values in the passed std::vector
* @param primes The vector that has been passed through `sieve(...)`
* @returns void
*/
void print_primes(std::vector<bool> const &primes) {
for (uint64_t i = 0; i < primes.size(); i++) {
if (primes[i]) {
std::cout << i << std::endl;
}
}
}
} // namespace math
/**
* @brief Self-tests the sieve function for major inconsistencies
* @returns void
*/
static void test() {
auto primes = std::vector<bool>(10, true);
math::sieve(&primes);
assert(primes[0] == false);
assert(primes[1] == false);
assert(primes[2] == true);
assert(primes[3] == true);
assert(primes[4] == false);
assert(primes[5] == true);
assert(primes[6] == false);
assert(primes[7] == true);
assert(primes[8] == false);
assert(primes[9] == false);
std::cout << "All tests have successfully passed!\n";
}
/**
* @brief Main function
* @param argc commandline argument count
* @param argv commandline array of arguments
* @returns 0 on exit
*/
int main(int argc, char *argv[]) {
test(); // run self-test implementations
// The largest prime we will check for
auto max = 10000;
// Store a boolean for every number which states if that index is prime or
// not
auto primes = std::vector<bool>(max, true);
// Store the algorithm start time
auto start = std::chrono::high_resolution_clock::now();
// Run the sieve
math::sieve(&primes);
// Time difference calculation
auto time = std::chrono::duration_cast<
std::chrono::duration<double, std::ratio<1>>>(
std::chrono::high_resolution_clock::now() - start)
.count();
// Print the primes if we see that "print" was passed as an arg
if (argc > 1 && argv[1] == std::string("print")) {
math::print_primes(primes);
}
// Print the time taken we found earlier
std::cout << "Time taken: " << time << " seconds" << std::endl;
return 0;
}