Skip to content

Instantly share code, notes, and snippets.

@ranisalt
Created September 23, 2015 20:44
Show Gist options
  • Save ranisalt/0b13cbc551b9d8c40d89 to your computer and use it in GitHub Desktop.
Save ranisalt/0b13cbc551b9d8c40d89 to your computer and use it in GitHub Desktop.
Sieve
#include <algorithm>
#include <cstdint>
#include <iostream>
#include <iterator>
#include <limits>
#include <vector>
int main(int argc, char** argv) {
auto n = 100000U;
auto sieve = std::vector<uint32_t>(n);
std::iota(begin(sieve), end(sieve), 2);
auto last = end(sieve);
for (auto it = begin(sieve); it != last; ++it) {
last = std::remove_if(it + 1, last, [&it](uint32_t n) {
return n % *it == 0;
});
}
sieve.erase(last, end(sieve));
for (const auto& n: sieve) {
std::cout << n << " ";
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment