|
Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
|
The Sieve of Eratosthenes More...
#include <cassert>#include <chrono>#include <iostream>#include <string>#include <vector>Namespaces | |
| namespace | math |
| for IO operations | |
Functions | |
| void | math::sieve (std::vector< bool > *vec) |
| Performs the sieve. | |
| void | math::print_primes (std::vector< bool > const &primes) |
| Prints all the indexes of true values in the passed std::vector. | |
| static void | test () |
| Self-tests the sieve function for major inconsistencies. | |
| int | main (int argc, char *argv[]) |
| Main function. | |
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
| int main | ( | int | argc, |
| char * | argv[] ) |
Main function.
| argc | commandline argument count |
| argv | commandline array of arguments |
|
static |
Self-tests the sieve function for major inconsistencies.