Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
eratosthenes.cpp File Reference

The Sieve of Eratosthenes More...

#include <cassert>
#include <chrono>
#include <iostream>
#include <string>
#include <vector>
Include dependency graph for eratosthenes.cpp:

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.
 

Detailed Description

The Sieve of Eratosthenes

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

Function Documentation

◆ main()

int main ( int argc,
char * argv[] )

Main function.

Parameters
argccommandline argument count
argvcommandline array of arguments
Returns
0 on exit
87 {
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
99
100 // Run the sieve
102
103 // Time difference calculation
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")) {
112 }
113
114 // Print the time taken we found earlier
115 std::cout << "Time taken: " << time << " seconds" << std::endl;
116
117 return 0;
118}
T duration_cast(T... args)
T endl(T... args)
static void test()
Self-tests the sieve function for major inconsistencies.
Definition eratosthenes.cpp:64
T max(T... args)
void sieve(std::vector< bool > *vec)
Performs the sieve.
Definition eratosthenes.cpp:33
void print_primes(std::vector< bool > const &primes)
Prints all the indexes of true values in the passed std::vector.
Definition eratosthenes.cpp:51
std::vector< int > primes(size_t max)
Definition prime_numbers.cpp:12
T time(T... args)
Here is the call graph for this function:

◆ test()

static void test ( )
static

Self-tests the sieve function for major inconsistencies.

Returns
void
64 {
65 auto primes = std::vector<bool>(10, true);
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}
Here is the call graph for this function: