![]() |
TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
|
The Sieve of Eratosthenes More...
#include <cassert>#include <chrono>#include <iostream>#include <string>#include <vector>Go to the source code of this file.
Namespaces | |
| namespace | math |
| for assert | |
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
Definition in file eratosthenes.cpp.
| int main | ( | int | argc, |
| char * | argv[] ) |
Main function.
| argc | commandline argument count |
| argv | commandline array of arguments |
Definition at line 87 of file eratosthenes.cpp.
|
static |
Self-tests the sieve function for major inconsistencies.
Definition at line 64 of file eratosthenes.cpp.