Skip to content

Commit

Permalink
Create Sieve_of_Eratosthenes.cpp
Browse files Browse the repository at this point in the history
Signed-off-by: Akash <[email protected]>
  • Loading branch information
Akash1437 committed Oct 17, 2022
1 parent c2c8348 commit d0364c8
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions Sieve_of_Eratosthenes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

#include <bits/stdc++.h>
using namespace std;

void SieveOfEratosthenes(int n)
{
bool prime[n+1];
memset(prime, true, sizeof(prime));

for (int p=2; p*p<=n; p++)
{
if (prime[p] == true)
{
for (int i=p*2; i<=n; i += p)
prime[i] = false;
}
}
for (int p=2; p<=n; p++)
if (prime[p])
cout << p << " ";
}

int main()
{
int n = 30;
cout << "Following are the prime numbers smaller "
<< " than or equal to " << n << endl;
SieveOfEratosthenes(n);
return 0;
}

0 comments on commit d0364c8

Please sign in to comment.