From 8b330ac4392ab61fd9d3fca296e162182c7aa4f9 Mon Sep 17 00:00:00 2001 From: yugantar99 <73051921+yugantar99@users.noreply.github.com> Date: Sun, 18 Oct 2020 12:37:13 +0530 Subject: [PATCH] sieve.cpp Finding prime numbers in nloglogn complexity --- C++/sieve.cpp | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 C++/sieve.cpp diff --git a/C++/sieve.cpp b/C++/sieve.cpp new file mode 100644 index 0000000..f3617fe --- /dev/null +++ b/C++/sieve.cpp @@ -0,0 +1,32 @@ +//Program to find the prime numbers in O(nloglogn) +#include +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; +}