Last active
December 16, 2015 04:09
-
-
Save fedelebron/5375544 to your computer and use it in GitHub Desktop.
A small implementation of Eratosthenes' sieve.
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> | |
#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