Skip to content

Commit

Permalink
Create Sieve_of_Eratosthenes.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
AvinashBonthu authored Oct 2, 2020
1 parent 6795359 commit 71e0e4b
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions C and C++/Sieve_of_Eratosthenes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#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*p; i<=n; i += p)
prime[i] = false;
}
}
for (int p=2; p<=n; p++)
if (prime[p])
cout << p << " ";
}
int main()
{
int a = 30;
cout << "Following are the prime numbers smaller "
<< " than or equal to " << a << "\n";
SieveOfEratosthenes(a);
return 0;
}

0 comments on commit 71e0e4b

Please sign in to comment.