PrimeVisit : time limit error and second case wrong

.

Hi @Number_Rajat1012,
I think you have tagged the wrong content. But going by the question (Prime Visits) it is a simple question.

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), try and 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.

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.