fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. void soe(int n) {
  5. vector<bool> p(n+1, true);
  6. for (int x=2;x*x<=n;x++) {
  7. if (p[x]) {
  8.  
  9. for (int y=x*x;y<=n;y+=x)
  10. {
  11. p[y]=false;
  12.  
  13. }
  14. }
  15. }
  16. for (int x=2;x<=n;x++) {
  17. if (p[x]) {
  18. cout<<x<<" ";
  19. }
  20. }
  21. }
  22.  
  23. int main() {
  24. int n=30;
  25. soe(n);
  26. return 0;
  27. }
  28.  
Success #stdin #stdout 0.01s 5300KB
stdin
Standard input is empty
stdout
2 3 5 7 11 13 17 19 23 29