Please share the question link. From the code it looks like you are trying to find all the primes between a and b. If so, your approach will time out. Just give a large a and b and run your code on your system. You can see how long it takes.
Then study segmented sieve and implement that. If you don’t know sieve, start with sieve-of-eratosthenes and do a couple of problems from that. Then move to segmented sieve.