Skip to content

Instantly share code, notes, and snippets.

@zwpp
Last active August 24, 2016 12:32
Show Gist options
  • Save zwpp/6b5aa112cfc4208edb59d0c582eaafe5 to your computer and use it in GitHub Desktop.
Save zwpp/6b5aa112cfc4208edb59d0c582eaafe5 to your computer and use it in GitHub Desktop.
/*
* 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