Last active
August 24, 2016 12:32
-
-
Save zwpp/6b5aa112cfc4208edb59d0c582eaafe5 to your computer and use it in GitHub Desktop.
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
/* | |
* Sample of "Sieve of Eratosthenes". | |
* Require:C++14 | |
*/ | |
#include <iostream> | |
#include <vector> | |
#include <algorithm> | |
#include <numeric> | |
int main() | |
{ | |
std::vector<unsigned> v(999999); | |
std::iota(v.begin(), v.end(), 2); | |
v.erase(std::remove_if(v.begin(), v.end(), | |
[&](auto n){ | |
for (auto itr = v.begin(); (*itr) * (*itr) <= n; ++itr) { | |
if (n % *itr == 0) return true; | |
} | |
return false; | |
}), v.end()); | |
v.shrink_to_fit(); | |
for (auto var : v) { | |
std::cout << var << std::endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment