fork download
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. int main()
  5. {
  6.  
  7. int n;
  8. scanf("%d",&n);
  9.  
  10. int arr[n+1] = {0};
  11.  
  12. arr[1] = 1;
  13.  
  14. for(int i = 4 ; i<=n ; i+=2)
  15. arr[i] = 1;
  16.  
  17.  
  18. for(int i = 3; i<=sqrt(n) ; i+=2)
  19. {
  20. if(arr[i] == 0)
  21. for(int j = i*i ; j<=n ; j+=2*i)
  22. {
  23. arr[j] = 1;
  24. }
  25. }
  26.  
  27. for(int i = 2 ; i<=n ; i++){
  28. if(arr[i] == 0)printf("%d ",i);
  29. }
  30. printf("\n");
  31.  
  32. }
  33.  
Success #stdin #stdout 0s 4328KB
stdin
50
stdout
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47