error is coming in this code array index out of bounds
Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: -2146737495
at Main.primesieve(Main.java:31)
at Main.main(Main.java:6)
Prime visits error in code
@amoghsachdeva3,
I have corrected your sieve method. Here is the code. https://ide.codingblocks.com/s/202303
In sieve you have to start from 2, rather than 3. Also there were trivial mistakes in the sieve method.
But this code can give you run time error. Instead of using csum, simply count the prime numbers between a and b using the array ‘p’. csum method can go out of bound on certain test cases and throw errors.
i followed this method explained in prime visits video https://youtu.be/8sniiDbfPK4?list=PLl4Y2XuUavmuFLS-RJCGUYiJNMw2wQYKX in this they have taken loop from 3 . why have you taken loop from 2 to i*i . i was not able to assign long value for j and i inside for loop it was showing error
the same code works with csum method in c++ but not in java why?
@amoghsachdeva3,
That won’t matter much since in sieve we mark all the non primes. If you start with 2 you can easily eliminate all the even numbers. If you start from 3, numbers such as 6,12,18… will be eliminated.
The same in java will give run time error if a=0, because it will give an out of bounds exception because:
int x = csum[b] - csum[a - 1];
In the above line of code we are doing a-1, hence if a is 0. It will become csum[-1], which is a runtime error.
The same code works in CPP because, in CPP if you try to access an invalid index or say arr[-1] itself, it returns 0 and does not throw any error, but in Java you will get an error(Array Index Out Of Bounds Exception) for this.
If you have any further queries I would be happy to help 
thank u for answering mu doubts were resolved