forked from Prince-1501/Hello_world-Competiitve-Programming
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Lecture - 24.cpp
43 lines (39 loc) · 837 Bytes
/
Lecture - 24.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
//
// main.cpp
// Lecture - 24
//
// Created by Prince Kumar on 28/03/19.
// Copyright © 2019 Prince Kumar. All rights reserved.
// seive of eratosthenes -- //
#include <iostream>
#include <cstring>
using namespace std;
void seive( int n)
{
// create boolean array
bool prime[n+1];
memset(prime, true ,sizeof(prime));
for(int p=2;p*p<=n;p++)
{
// if prime[p] --> false --> it is not prime
if(prime[p]==true)
{
for(int i=p*p;i<=n;i=i+p)
{
prime[i]=false;
}
}
}
// print prime numbers
for(int j=2;j<=n;j++)
{
if(prime[j]==true)
cout<<j<<endl;
}
}
int main()
{
cout <<"Enter number upto which you want to print prime numbers "<<endl;
int n ; cin>>n;
seive(n);
}