test case 1 showing timelimit error
link to my code:
Prime visits : timelimit error
Hi @Bhavika_rastogi,
The approach you are using will give you TLE for sure. See, when you are checking for a prime number, you needn’t go till n-1 to find the prime number. What if you go only till n/2 (because every number after n/2 can never be divisible be n. Also, you can go till sqrt(n). Think why.
Now in this question you need to use a similar technique. To deal with TLE here you need to use Sieve technique where you create a boolean array of b+1 size. And start from the first prime number i.e. 2. Take its square(4) and increment it by the number you took (i.e. 4,6,8,10 and so on). Mark all these as false since they cannot be primes. Now take the next unmarked number, 3. And mark 9,12,15 and so as false. Similarly do it for 4. 16,20,24 and so on as false. When you finish the loop, count all the positions marked as true between a and b. This will be our final answer.