Deepak and primes II

3 test cases sre not getting passed its showing run time error,so how can i compute that

Hi @Shivam-Singla-618559708603791
pls share ur code using ide.codingblocks.com

this is my code
#include
#include
using namespace std;

void primesieve(int *p){
for(int i=3;i<=1000000;i+=2){
p[i]=1;
}
for(long long i=3;i<=1000000;i+=2){

   for(long long j=i*i;j<=1000000;j+=i){
       p[j]=0;
   }

}
p[0]=0;
p[1]=0;
p[2]=1;
}

int main() {

int  p[1000000]={0};

primesieve§;

int q;
cin>>q;
while(q–){
int a,b;
cin>>a>>b;
for(int i=a;i<=b;i++){
if(p[i]==1){
cout<<i<<endl;
}
}
}

return 0;

}

Hi @Shivam-Singla-618559708603791
in ur code, prime sieve function was not correctly identifying prime numbers hence ur otput was coming out to be wrong.
i have updated ur code.


refer to dis for updated prime sieve function.
Moreover in this question, u will noting that value of n can be >10^7. Hence u will hv to use segmented prime sieve to get all test cases correct.
Try applying segmented prime sieve in ur code.
If u face any problems or have doubts, u can post them here
Hope dis helps

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.