TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
sieve_of_eratosthenes.cpp File Reference

Prime Numbers using Sieve of Eratosthenes More...

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

Go to the source code of this file.

Namespaces

namespace  math
 for assert
 
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

Definition in file sieve_of_eratosthenes.cpp.

Function Documentation

◆ main()

int main ( void )

Main function.

Returns
0 on exit

Definition at line 119 of file sieve_of_eratosthenes.cpp.

119 {
120 tests();
121 return 0;
122}
static void tests()
Self-test implementations.

◆ 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

Definition at line 65 of file sieve_of_eratosthenes.cpp.

65 {
66 for (uint32_t i = 2; i <= N; i++) {
67 if (is_prime[i]) {
68 std::cout << i << ' ';
69 }
70 }
71 std::cout << std::endl;
72}
constexpr uint32_t N
A struct to represent sparse table for min() as their invariant function, for the given array A....

◆ 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

Definition at line 45 of file sieve_of_eratosthenes.cpp.

45 {
46 std::vector<bool> is_prime(N + 1, true); // Initialize all as prime numbers
47 is_prime[0] = is_prime[1] = false; // 0 and 1 are not prime numbers
48
49 for (uint32_t i = 2; i * i <= N; i++) {
50 if (is_prime[i]) {
51 for (uint32_t j = i * i; j <= N; j += i) {
52 is_prime[j] = false;
53 }
54 }
55 }
56 return is_prime;
57}
bool is_prime(int64_t num)
Function to check if the given number is prime or not.

◆ tests()

static void tests ( )
static

Self-test implementations.

Returns
void

Definition at line 81 of file sieve_of_eratosthenes.cpp.

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