Skip to content

Instantly share code, notes, and snippets.

@ygabo
Created September 14, 2013 10:20
Show Gist options
  • Save ygabo/6560811 to your computer and use it in GitHub Desktop.
Save ygabo/6560811 to your computer and use it in GitHub Desktop.
Sieve of Eratosthenes.
#include <iostream>
using namespace std;
#include <list>
void TA(int n){
list<int> m;
m.push_back(2);
for( int i = 3; i <= n; i+=2)
m.push_back(i);
int x, y;
for(auto i = m.begin(); i!= m.end(); ++i){
x = *i;
auto j = i;
++j;
for(;j != m.end();){
y = *j;
if( (y % x) == 0 ){
j = m.erase(j);
}
else{
++j;
}
}
}
cout << endl;
for( auto i = m.begin(); i != m.end(); ++i)
cout << *i << " ";
cout << endl;
}
int main(){
int n = 26;
TA(n);
cout << endl << "done." << endl;
std::cin.get();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment