Skip to content

Commit

Permalink
Merge pull request rathoresrikant#550 from z3r0dmg/patch-4
Browse files Browse the repository at this point in the history
Linear Sieve
  • Loading branch information
Srikant Singh authored Oct 27, 2018
2 parents b2283f7 + d75d8fd commit 1e9a698
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions Algorithms/Mathematical Algorithms/LinearSieve.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <bits/stdc++.h>

using namespace std;

const int N = 10000000;
int lp[N+1];
vector<int> prime;

void sieve(){
for (int i=2; i<=N; ++i) {
if (lp[i] == 0) {
lp[i] = i;
prime.push_back (i);
}
for (int j=0; j<(int)prime.size() && prime[j]<=lp[i] && i*prime[j]<=N; ++j)
lp[i * prime[j]] = prime[j];
}
}

int main(){

sieve();
int n;
cin>>n;

for(int i=0;i<=n;i++){
cout<<prime[i]<<" ";
}
cout<<'\n';
}

0 comments on commit 1e9a698

Please sign in to comment.