Advertisement
Oibek

Sieve of Eratosthenes

Mar 25th, 2018
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long double ld;
  5.  
  6. int main() {
  7.     ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0);
  8.  
  9.     // Решето по нечетным числам
  10.     ld n; cin >> n;
  11.     vector<bool> v((n-1)/2+1, 1);
  12.  
  13.     for (ld i = 3; i*i<=n; i+=2)
  14.         if (v[(i-1)/2])
  15.             for (ld j = i*i; j<=n; j+=2*i)
  16.                 v[(j-1)/2] = 0;
  17.  
  18.     cout << "2 ";
  19.     for (ld i = 1; i<v.size(); i++)
  20.         if (v[i]) cout << 2*i+1 << ' ';
  21.  
  22.     return 0;
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement