Skip to content

Commit

Permalink
Merge pull request #102 from Sindhu-321/main
Browse files Browse the repository at this point in the history
Added Sieve of eratosthenes algorithm
  • Loading branch information
aman-raza authored Oct 23, 2020
2 parents 5dd76cd + 1cfcc2b commit dc14603
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions CPP/Sieve_of_Eratosthenes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <iostream>

using namespace std;
#include <vector>

vector<long long int> soe(long long int a){
vector<bool> ref(a+1);
vector<long long int> ans;
int i=2;
ans.push_back(2);
while(i<=a){
for(int j=i*i;j<=a;j+=i){
if(!ref[j])ref[j]=true;
}
i++;
while(ref[i])i++;
if(i<=a) ans.push_back(i);

}
return ans;

}

int main()
{
long long int n;
cout<<"enter number"<<endl;
cin>>n;
vector<long long int> primes=soe(n);
cout<<"The primes which are less than or equal to "<<n<<" are:"<<endl;
for(int i=0;i<primes.size();i++)cout<<primes[i]<<" ";
cout<<endl;

}

0 comments on commit dc14603

Please sign in to comment.