fork download
  1. /*
  2.   Cred : SunnyYeahBoi
  3.   It's my last chance (⌐■_■)
  4.   Problem :
  5. */
  6.  
  7. #include<bits/stdc++.h>
  8.  
  9. using namespace std;
  10.  
  11. #define int long long
  12. #define double long double
  13. #define endl "\n"
  14. #define NAME "a"
  15.  
  16. const int MAXN = 1e6 + 5;
  17. const int inf = 1e18;
  18. const int MOD = 1e9 + 7;
  19.  
  20. void FileInput(){
  21. if(fopen(NAME".inp" , "r") == NULL)
  22. freopen(NAME".inp" , "w" , stdout);
  23. freopen(NAME".inp" , "r" , stdin);
  24. freopen(NAME".out" , "w" , stdout);
  25. }
  26.  
  27. int n;
  28.  
  29. const int N = 1e6 + 5;
  30. bool isPrime[N + 1];
  31.  
  32. void sang(){
  33. for(int i = 0 ; i <= N ; i++)
  34. isPrime[i] = true;
  35. isPrime[0] = isPrime[1] = false;
  36. for(int i = 2 ; i <= N ; i++){
  37. if(isPrime[i]){
  38. for(int j = 2 * i ; j <= N ; j += i) // 2i , 3i , 4i , ...
  39. isPrime[j] = false;
  40. }
  41. }
  42. }
  43.  
  44. void solve(){
  45. sang();
  46. cin >> n;
  47. for(int i = 2 ; i <= n ; i++){
  48. if(isPrime[i])
  49. cout << i << " ";
  50. }
  51. }
  52.  
  53. int32_t main(){
  54. FileInput();
  55. ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  56. int t = 1;
  57. // cin >> t;
  58. while(t--)
  59. solve();
  60. return 0;
  61. }
  62.  
Success #stdin #stdout 0.01s 5312KB
stdin
Standard input is empty
stdout
Standard output is empty