Created
September 14, 2013 10:20
-
-
Save ygabo/6560811 to your computer and use it in GitHub Desktop.
Sieve of Eratosthenes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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