schl

eratosten*

Jan 25th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.61 KB | None | 0 0
  1. /*
  2.  * efficient up to 10e7 (struggles with 10e8, takes a few seconds to finish)
  3.  * arb array has to be set to true (with memset or using "static" keyword)
  4.  * n represents the upper limit in the range (0..n)
  5.  * prime vector was made in global scope
  6. */
  7.  
  8. void eratosten(bool arb[], ll n){
  9.     for(ll chk = 2; chk < n; chk++){
  10.         if(!arb[chk]) continue;
  11.             prime.push_back(chk);
  12.             arb[chk] = true;
  13.  
  14.         for(ll i = chk * 2; i < n; i+=chk){
  15.             if(arb[i]){
  16.                 if(i % chk != 0) arb[i] = true;
  17.                 else arb[i] = false;
  18.             }
  19.         }
  20.     }
  21. }
Advertisement
Add Comment
Please, Sign In to add comment