Add files via upload

pull/1195/head
Rohan Janardhan 2023-06-02 10:04:08 +05:30 committed by GitHub
parent af47764be0
commit 6c25653f10
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 34 additions and 0 deletions

View File

@ -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<bits/stdc++.h>
using namespace std;
vector<int> primes;
bool check[INT_MAX];
void LinearSieve(int n) {
fill(check, check+n, false);
for(int i=2;i<n;i++){
if(!check[i]) primes.push_back(i);
for(int j=0;j<primes.size() && i*primes[j] < n;j++){ //Executed only once for each composite
check[i*primes[j]] = true;
if(i%primes[j]==0) break; //Breaks when p divides i
}
}
}
int main() {
int n;
cin >> 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
}