Skip to content

Commit

Permalink
Merge pull request Burnout-Devil#18 from Akash1437/patch-3
Browse files Browse the repository at this point in the history
Create sieve_of_eratosthenes.cpp
  • Loading branch information
Burnout-Devil committed Oct 17, 2022
2 parents 8589465 + ddc74b8 commit d1680cc
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions sieve_of_eratosthenes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

#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 d1680cc

Please sign in to comment.