Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions C++/sieve.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//Program to find the prime numbers in O(nloglogn)
#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 << " ";
}

// Driver Program
int main()
{
int n = 1000;
cout << "Here are the prime numbers smaller "
<< " than or equal to " << n << endl;
SieveOfEratosthenes(n);
return 0;
}