Skip to content

Instantly share code, notes, and snippets.

@firegurafiku
Created October 30, 2016 23:06
Show Gist options
  • Save firegurafiku/8f1879eb8ddabc01342211e9d7bb9718 to your computer and use it in GitHub Desktop.
Save firegurafiku/8f1879eb8ddabc01342211e9d7bb9718 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
const int N = 10000000;
int main() {
// Let the seive be an array (really, std::vector<bool> is not exactly
// an array, but that's not important now) of size N with all elements
// initially set to 'true' istead of 'false'.
std::vector<bool> sieve(N, true);
for (int i=2; i <= N / 2; ++i) {
if (sieve[i]) {
for (int j=i+i; j < N; j += i)
sieve[j] = false;
}
}
for (int i=2; i < N; ++i) {
if (sieve[i])
std::cout << i << std::endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment