Skip to content

Commit 8fc9673

Browse files
authored
Merge pull request ephremdeme#127 from Kanhakhatri065/master
Implemented Sieve of Eratosthenes in C++
2 parents f5a182d + 05629f6 commit 8fc9673

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
const int MAX = 1e5 + 5;
6+
bool sieve[MAX];
7+
void sieveOfEratosthenes() {
8+
memset(sieve, true, sizeof(sieve));
9+
sieve[0] = sieve[1] = false;
10+
for(int i = 2;i < MAX;i++) {
11+
if(sieve[i] == true) {
12+
for(int j = 2 * i;j < MAX;j += i) {
13+
sieve[j] = false;
14+
}
15+
}
16+
}
17+
}
18+
19+
int main() {
20+
int n;
21+
cin >> n;
22+
23+
sieveOfEratosthenes();
24+
25+
if(sieve[n] == true) {
26+
cout << "Prime" << endl;
27+
} else {
28+
cout << "Not Prime" << endl;
29+
}
30+
31+
return 0;
32+
}

0 commit comments

Comments
 (0)