Advertisement
istinishat

segmented_seive

Oct 7th, 2017
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // first call seive() ,
  2. //this fun returns no. of prime between [a,b] interval
  3.  
  4. int segment_seive(ll a,ll b)
  5. {
  6.     if(b<2)return 0;
  7.     if(a<2)a=2;
  8.     bool mark[b-a+1];
  9.     memset(mark,true,sizeof(mark));
  10.  
  11.  
  12.     for(int i=0;i<prime.size() && (ll)prime[i]*prime[i]<=b;i++){
  13.         ll j=(a/prime[i])*prime[i];
  14.         if(j<a)
  15.             j+=prime[i];
  16.         if(j<(ll)prime[i]*2)j=prime[i]*2;
  17.         for(;j<=b;j+=prime[i]){
  18.             mark[j-a]=false;
  19.         }
  20.     }
  21.     int ret=0;
  22.     for(int i=a;i<=b;i++){
  23.         ret+=mark[i-a];
  24.     }
  25.  
  26.     return ret;
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement