Skip to content

Commit ac4f4a3

Browse files
Create simple_sieve.cpp
1 parent 676fe97 commit ac4f4a3

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

simple_sieve.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
void SieveOfEratosthenes(int n)
5+
{
6+
bool prime[n + 1];
7+
memset(prime, true, sizeof(prime));
8+
9+
for (int p = 2; p * p <= n; p++) {
10+
if (prime[p] == true) {
11+
for (int i = p * p; i <= n; i += p)
12+
prime[i] = false;
13+
}
14+
}
15+
for (int p = 2; p <= n; p++)
16+
if (prime[p])
17+
cout << p << " ";
18+
}
19+
int main()
20+
{
21+
int n = 30;
22+
cout << "Following are the prime numbers smaller "
23+
<< " than or equal to " << n << endl;
24+
SieveOfEratosthenes(n);
25+
return 0;
26+
}

0 commit comments

Comments
 (0)