diff --git a/algorithms/CPlusPlus/Maths/linear-sieve.cpp b/algorithms/CPlusPlus/Maths/linear-sieve.cpp new file mode 100644 index 00000000..00dad4bf --- /dev/null +++ b/algorithms/CPlusPlus/Maths/linear-sieve.cpp @@ -0,0 +1,34 @@ +//Linear Sieve +//Finding primes in linear time + +//Every composite q must have atleast 1 prime factor +//Let the smallest prime factor be p +//We can say q = p*(i) + +//When we loop for every i, all primes not exceeding i is already stored in primes +//We can pick out each composite exactly once by looping and breaking out when the element divides i + +#include +using namespace std; + +vector primes; +bool check[INT_MAX]; + + +void LinearSieve(int n) { + fill(check, check+n, false); + for(int i=2;i> n; + LinearSieve(n); //Call the function + return 0; //Time complexity : O(n*log(log(n))), Auxiliary Space: O(1) + //Example : n=10 , Output: 2 3 5 7 +} \ No newline at end of file