Advertisement
Araf_12

bitset seive

Mar 16th, 2021 (edited)
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.55 KB | Source Code | 0 0
  1.  
  2. const int MX = 1e7+123;
  3. bitset<MX> is_prime;
  4. vector<int> prime;
  5.  
  6. void primeGen ( int n )
  7. {
  8.     n += 100;
  9.     for ( int i = 3; i <= n; i += 2 ) is_prime[i] = 1;
  10.  
  11.     int sq = sqrt ( n );
  12.     for ( int i = 3; i <= sq; i += 2 ) {
  13.         if ( is_prime[i] == 1 ) {
  14.             for ( int j = i*i; j <= n; j += ( i + i ) ) {
  15.                 is_prime[j] = 0;
  16.             }
  17.         }
  18.     }
  19.  
  20.     is_prime[2] = 1;
  21.     prime.push_back (2);
  22.  
  23.     for ( int i = 3; i <= n; i += 2 ) {
  24.         if ( is_prime[i] == 1 ) prime.push_back ( i );
  25.     }
  26. }
  27.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement