forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sieve_of_Eratosthenes.cpp
117 lines (98 loc) · 2.6 KB
/
Sieve_of_Eratosthenes.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <iostream> //Header file
using namespace std; //For cin and cout
/*
Sieve of Eratosthenes is an ancient algorithm for finding all prime numbers up to any given limit.
It does so by iteratively marking as composite (i.e., not prime) the multiples of each prime, starting with
the first prime number, 2. The multiples of a given prime are generated as a sequence of numbers starting from that prime,
with constant difference between them that is equal to that prime.
Sample Input :
10
Sample Output:
2 3 5 7
@author Aditya Saxena
@since 28-7-2020
*/
//Implement prime sieve function, it will store 0 in the primes array if that index is composite and 1 if prime
void prime_sieve(int primes[], int n)
{
//Make 2 as prime (only prime even number)
primes[2] = 1;
//Put 1 at all odd positions starting from 3
for (int i = 3; i < n; i += 2)
{
primes[i] = 1;
}
//Make all multiples of odds as composite, i.e, 0
for (int i = 3; i < n; i += 2)
{
//Check prime(i.e, 1){
if (primes[i] == 1)
{
//Make all multiples of i as composite
for (int j = i * i; j < n; j += i)
{
primes[j] = 0;
}
}
}
return;
}
/**
* This is the Optimized version of Sieve of Eratosthenes using Dynamic Memory Allocation and using Boolean that finds the primes and eliminates
* the multiples.
*/
void sieve(int N, bool *isprime)
{
isprime[0] = true;
isprime[1] = true;
for (int i = 2; i * i <= N; ++i)
{
if (!isprime[i])
{
for (int j = i * i; j <= N; j = j + i)
{
isprime[j] = true;
}
}
}
}
//This function prints out the primes to STDOUT.
void print(int N, bool *isprime)
{
for (int i = 1; i <= N; i++)
{
if (!isprime[i])
{
std::cout << i << ' ';
}
}
std::cout << '\n';
}
//Driver Function
int main()
{
int n = 10;
cout << "Enter the number upto which prime numbers are to be computed: ";
cin >> n;
//Create an array of size n+1 initialized with 0
n++;
int primes[n] = {0};
//Call prime sieve function
prime_sieve(primes, n);
//Print all primes upto n
cout << endl;
for (int i = 0; i < n; i++)
{
if (primes[i] == 1)
{
cout << i << " ";
}
}
//Implementation of Optimized version of Sieve of Eratosthenes
int N = 100;
bool *isprime = new bool[N];
sieve(N, isprime);
print(N, isprime);
delete[] isprime;
return 0;
}