fork download
  1. #include <iostream>
  2. #include <set>
  3. using namespace std;
  4.  
  5. int main() {
  6. int n = 100;
  7. set<int> a;
  8. set<int>::iterator pos = a.begin();
  9.  
  10. //generate set of values 1-10
  11. for (int i = 2; i <= n; i++) {
  12. a.insert(i);
  13. if(pos != a.end())
  14. pos++;
  15. }
  16. pos = a.begin();
  17. while (pos != a.end()) {
  18. int current = *pos++;
  19. int remove = current + current;
  20. while (remove <= n) {
  21. a.erase(remove);
  22. remove += current;
  23. }
  24. }
  25. pos = a.begin();
  26. while (pos != a.end()) {
  27. cout << *pos << " ";
  28. pos++;
  29. }
  30. cout << endl;
  31. return 0;
  32. }
Success #stdin #stdout 0s 3412KB
stdin
Standard input is empty
stdout
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97