Getting error even after using prime sieve method. below is the link to the code

hey @shikhar07
inside the primeSieve function line no 42
for(int i=3; i* i<=1000000;i=i+2) // instead of for(int i=3;i*i<=1000000;i=i+2)

and this wrong
for(int i=1;i<=1000000;i++)
{
if(p[i])
{
preSum[i]=preSum[i-1]+1;
}
}
No need

correct code :

yes your corrected code is accepted but i dont get why mine was not because what i tried for is O(q+n) solution and the solution you provided was O(q*n). Could you tell me what went wrong in my code.

try for this input :
1
1 10
your code gives : 0

yes i know , and for the input 10 20 i am getting -1 ,even though my logic is correct i couldnt debug what is going wrong in the code and exactly thats why i asked this , my idea is to precompute the number of primes till a number n from before hand , Eg:- preSum[5] will have total number of primes numbers present between 0 and 5 , so when me get the input a and b my answer would be preSum[b]-preSum[a];

for(int i=1;i<=1000000;i++)
{
if(p[i])
{
preSum[i]=preSum[i-1]+1;
}
// if p[i] is not prime previous sum store hoga
else {
preSum[i]=preSum[i-1];
}
}
and
if(p[a]) { // if(p[a] is prime include this answer : so , +1 kr do
System.out.println(preSum[b]-preSum[a]+1);
}
else
System.out.println(preSum[b]-preSum[a]);
correct code A/C to your Approach :

ok Thankyou , its working fine now …one doubt i had while trying to debug was this :- public static void main(String[] args)
{
boolean [] b=new boolean[10];
long i=0;
b[i]=true; // why does this line give compilation error?
int j=0;
b[j]=0;
b[j]=true; // and this line doesnt
}
could you tell me why ?

b[i]=true; // why does this line give compilation error?
Array index must be int values

okay , could you explain why?

java me Heap Space ka problem aata h

1 Like