Skip to content

Commit 9c92ea6

Browse files
Add files via upload
Seive of Eratosthenes is a concept mainly to find the prime number in given range in time complexity of O(loglogn) inspite of O(n^2). In this approach, we just mark all elements as prime and then we unmark them as they are divided and elements left at last are all prime numbers.
1 parent 0ae7b24 commit 9c92ea6

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

18_seiveofEratosthenes.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <iostream>
2+
using namespace std;
3+
void seiveOfEratosthenes(int n){
4+
int *a = new int[n]{0};
5+
for(int i=2; i<=n; i++){
6+
if(a[i] == 0){
7+
for(int j=i*i; j<=n; j+=i){
8+
a[j] = 1;
9+
}
10+
}
11+
}
12+
for(int i=2; i<=n; i++){
13+
if(a[i]==0){
14+
cout<<i<<" ";
15+
}
16+
}
17+
}
18+
int main(){
19+
// Seive of Eratosthenes is a concept mainly to find the prime number in given range in time complexity of O(loglogn) inspite of O(n^2). In this approach, we just mark all elements as prime and then we unmark them as they are divided and elements left at last are all prime numbers.
20+
int n;
21+
cout<<"Enter the value of n upto which prime numbers are to be printed: ";
22+
cin>>n;
23+
24+
seiveOfEratosthenes(n);
25+
26+
27+
return 0;
28+
}

18_seiveofEratosthenes.exe

44.7 KB
Binary file not shown.

0 commit comments

Comments
 (0)