Created
September 23, 2015 20:44
-
-
Save ranisalt/0b13cbc551b9d8c40d89 to your computer and use it in GitHub Desktop.
Sieve
This file contains hidden or 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 <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