Skip to content

Instantly share code, notes, and snippets.

@fedelebron
Last active December 16, 2015 04:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fedelebron/5375544 to your computer and use it in GitHub Desktop.
Save fedelebron/5375544 to your computer and use it in GitHub Desktop.
A small implementation of Eratosthenes' sieve.
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n;
cin >> n;
vector<bool> composite(n);
composite[0] = true; // Technically a zero divisor
composite[1] = true;
for (int i = 2; i*i < n; ++i) {
if (composite[i]) continue;
for (int j = i*i; j < n; j += i) {
composite[j] = true;
}
}
for (int i = 0; i < n; ++i) {
if (!composite[i]) {
cout << i << endl;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment