TheAlgorithms/C++ 1.0.0
All the 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:

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.
 

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

Definition in file eratosthenes.cpp.

Function Documentation

◆ main()

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

Main function.

Parameters
argccommandline argument count
argvcommandline array of arguments
Returns
0 on exit

Definition at line 87 of file eratosthenes.cpp.

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
98 auto start = std::chrono::high_resolution_clock::now();
99
100 // Run the sieve
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")) {
112 }
113
114 // Print the time taken we found earlier
115 std::cout << "Time taken: " << time << " seconds" << std::endl;
116
117 return 0;
118}
static void test()
Self-tests the sieve function for major inconsistencies.
void sieve(std::vector< bool > *vec)
Performs the sieve.
void print_primes(std::vector< bool > const &primes)
Prints all the indexes of true values in the passed std::vector.
std::vector< int > primes(size_t max)

◆ test()

static void test ( )
static

Self-tests the sieve function for major inconsistencies.

Returns
void

Definition at line 64 of file eratosthenes.cpp.

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}