forked from hardikagarwal2001/Hackoctober
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseive.cpp
More file actions
30 lines (28 loc) · 637 Bytes
/
seive.cpp
File metadata and controls
30 lines (28 loc) · 637 Bytes
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
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll ;
int main()
{
long long n ; cin>>n ;
// Here n is the number upto which we have to find the prime numbers
bool arr[n+1] ;
for(int i = 0 ; i <=n ; i++){
arr[i] = true ;
}
arr[0] = false ;
arr[1] = false ;
for(long long i = 2 ; i <= n; i++){
if(arr[i] && i*i <=n ){
for(long long j = i*i ; j <= n ; j+=i){
arr[j] = false ;
}
}
}
for(int i = 1 ; i <= n ; i++){
if(arr[i]){
cout<<i<<" " ;
}
}
return 0;
}