Doubt related prime visit


this code is showing timelimit how can I improve my code?

@Mansi,

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 5,25,35,55 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.

1 Like

can you please send the code ,i am not getting the explaination?

@Mansi,
https://ide.codingblocks.com/s/169455 I have shared the code with you. Kindly have a look :slight_smile:

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.