A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://TheAlgorithms.github.io/C-Plus-Plus/d7/da6/eratosthenes_8cpp_source.html below:

TheAlgorithms/C++: math/eratosthenes.cpp Source File

Go to the documentation of this file. 33void sieve

(std::vector<bool> *vec) {

38 for

(uint64_t n = 2; n < vec->size(); n++) {

39 for

(uint64_t multiple = n << 1; multiple < vec->size();

41

(*vec)[multiple] =

false

;

52 for

(uint64_t i = 0; i <

primes

.size(); i++) {

54

std::cout << i << std::endl;

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

);

78

std::cout <<

"All tests have successfully passed!\n"

;

87int main

(

int

argc,

char

*argv[]) {

95 auto primes

= std::vector<bool>(max,

true

);

98 auto

start = std::chrono::high_resolution_clock::now();

104 auto

time = std::chrono::duration_cast<

105

std::chrono::duration<double, std::ratio<1>>>(

106

std::chrono::high_resolution_clock::now() - start)

110 if

(argc > 1 && argv[1] == std::string(

"print"

)) {

115

std::cout <<

"Time taken: "

<< time <<

" seconds"

<< std::endl;

static void test()

Self-tests the sieve function for major inconsistencies.

int main()

Main function.

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)


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4