diff --git a/C++/program-28/Readme b/C++/program-28/Readme new file mode 100644 index 00000000..aaa818e9 --- /dev/null +++ b/C++/program-28/Readme @@ -0,0 +1,30 @@ +Given a number n, print all primes smaller than or equal to n. It is also given that n is a small number. +The Sieve of Eratosthenes is one of the most efficient ways to find all primes smaller than n when n is smaller than 10 million or so. +Following is the algorithm to find all the prime numbers less than or equal to a given integer n by the Eratosthene’s method: + +When the algorithm terminates, all the numbers in the list that are not marked are prime. + +Explanation with Example: +1. Let us take an example when n = 50. So we need to print all prime numbers smaller than or equal to 50. +2. We create a list of all numbers from 2 to 50. +3. According to the algorithm we will mark all the numbers which are divisible by 2 and are greater than or equal to the square of it. +4. Now we move to our next unmarked number 3 and mark all the numbers which are multiples of 3 and are greater than or equal to the square of it. +5. We move to our next unmarked number 5 and mark all multiples of 5 and are greater than or equal to the square of it. +6. We move to our next unmarked number 5 and mark all multiples of 5 and are greater than or equal to the square of it. + + + +Example: Input : n =10 + Output : 2 3 5 7 + + Input : n = 20 + Output: 2 3 5 7 11 13 17 19 + + +MAIN OUTPUT:- + +Following are the prime numbers smaller than or equal to 30 +2 3 5 7 11 13 17 19 23 29 + + +Time complexity : O(n*log(log(n))) diff --git a/C++/program-28/Sieve_of_eratosthenes.cpp b/C++/program-28/Sieve_of_eratosthenes.cpp new file mode 100644 index 00000000..73b1cce3 --- /dev/null +++ b/C++/program-28/Sieve_of_eratosthenes.cpp @@ -0,0 +1,49 @@ +// C++ program to print all primes +// smaller than or equal to +// n using Sieve of Eratosthenes +#include +using namespace std; + +void SieveOfEratosthenes(int n) +{ + // Create a boolean array + // "prime[0..n]" and initialize + // all entries it as true. + // A value in prime[i] will + // finally be false if i is + // Not a prime, else true. + bool prime[n + 1]; + memset(prime, true, sizeof(prime)); + + for (int p = 2; p * p <= n; p++) + { + // If prime[p] is not changed, + // then it is a prime + if (prime[p] == true) + { + // Update all multiples + // of p greater than or + // equal to the square of it + // numbers which are multiple + // of p and are less than p^2 + // are already been marked. + for (int i = p * p; i <= n; i += p) + prime[i] = false; + } + } + + // Print all prime numbers + for (int p = 2; p <= n; p++) + if (prime[p]) + cout << p << " "; +} + +// Driver Code +int main() +{ + int n = 30; + cout << "Following are the prime numbers smaller " + << " than or equal to " << n << endl; + SieveOfEratosthenes(n); + return 0; +} \ No newline at end of file