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

Prime Numbers using Sieve of Eratosthenes More...

#include <cassert>
#include <iostream>
#include <vector>
Include dependency graph for sieve_of_eratosthenes.cpp:

Namespaces

namespace  math
 for IO operations
 
namespace  sieve_of_eratosthenes
 Functions for finding Prime Numbers using Sieve of Eratosthenes.
 

Functions

std::vector< bool > math::sieve_of_eratosthenes::sieve (uint32_t N)
 Function to sieve out the primes.
 
void math::sieve_of_eratosthenes::print (uint32_t N, const std::vector< bool > &is_prime)
 Function to print the prime numbers.
 
static void tests ()
 Self-test implementations.
 
int main ()
 Main function.
 

Detailed Description

Prime Numbers using Sieve of Eratosthenes

Sieve of Eratosthenes is an algorithm that finds all the primes between 2 and N.

Time Complexity : \(O(N \cdot\log \log N)\)
Space Complexity : \(O(N)\)

See also
primes_up_to_billion.cpp prime_numbers.cpp

Function Documentation

◆ main()

int main ( void )

Main function.

Returns
0 on exit
119 {
120 tests();
121 return 0;
122}
static void tests()
Self-test implementations.
Definition sieve_of_eratosthenes.cpp:80
Here is the call graph for this function:

◆ print()

void math::sieve_of_eratosthenes::print ( uint32_t N,
const std::vector< bool > & is_prime )

Function to print the prime numbers.

Parameters
Nnumber till which primes are to be found
is_primea vector of N + 1 booleans identifying if i^th number is a prime or not
64 {
65 for (uint32_t i = 2; i <= N; i++) {
66 if (is_prime[i]) {
67 std::cout << i << ' ';
68 }
69 }
71}
constexpr uint32_t N
A struct to represent sparse table for min() as their invariant function, for the given array A....
Definition sparse_table.cpp:47
T endl(T... args)
Here is the call graph for this function:

◆ sieve()

std::vector< bool > math::sieve_of_eratosthenes::sieve ( uint32_t N)

Function to sieve out the primes.

This function finds all the primes between 2 and N using the Sieve of Eratosthenes algorithm. It starts by assuming all numbers (except zero and one) are prime and then iteratively marks the multiples of each prime as non-prime.

Contains a common optimization to start eliminating multiples of a prime p starting from p * p since all of the lower multiples have been already eliminated.

Parameters
Nnumber till which primes are to be found
Returns
is_prime a vector of N + 1 booleans identifying if i^th number is a prime or not
44 {
45 std::vector<bool> is_prime(N + 1, true); // Initialize all as prime numbers
46 is_prime[0] = is_prime[1] = false; // 0 and 1 are not prime numbers
47
48 for (uint32_t i = 2; i * i <= N; i++) {
49 if (is_prime[i]) {
50 for (uint32_t j = i * i; j <= N; j += i) {
51 is_prime[j] = false;
52 }
53 }
54 }
55 return is_prime;
56}
bool is_prime(int64_t num)
Function to check if the given number is prime or not.
Definition check_prime.cpp:31
Here is the call graph for this function:

◆ tests()

static void tests ( )
static

Self-test implementations.

Returns
void
80 {
81 std::vector<bool> is_prime_1 =
82 math::sieve_of_eratosthenes::sieve(static_cast<uint32_t>(10));
83 std::vector<bool> is_prime_2 =
84 math::sieve_of_eratosthenes::sieve(static_cast<uint32_t>(20));
85 std::vector<bool> is_prime_3 =
86 math::sieve_of_eratosthenes::sieve(static_cast<uint32_t>(100));
87
88 std::vector<bool> expected_1{false, false, true, true, false, true,
89 false, true, false, false, false};
90 assert(is_prime_1 == expected_1);
91
92 std::vector<bool> expected_2{false, false, true, true, false, true,
93 false, true, false, false, false, true,
94 false, true, false, false, false, true,
95 false, true, false};
96 assert(is_prime_2 == expected_2);
97
98 std::vector<bool> expected_3{
99 false, false, true, true, false, true, false, true, false, false,
100 false, true, false, true, false, false, false, true, false, true,
101 false, false, false, true, false, false, false, false, false, true,
102 false, true, false, false, false, false, false, true, false, false,
103 false, true, false, true, false, false, false, true, false, false,
104 false, false, false, true, false, false, false, false, false, true,
105 false, true, false, false, false, false, false, true, false, false,
106 false, true, false, true, false, false, false, false, false, true,
107 false, false, false, true, false, false, false, false, false, true,
108 false, false, false, false, false, false, false, true, false, false,
109 false};
110 assert(is_prime_3 == expected_3);
111
112 std::cout << "All tests have passed successfully!\n";
113}
std::vector< bool > sieve(uint32_t N)
Function to sieve out the primes.
Definition sieve_of_eratosthenes.cpp:44