Prime visits question

i used the normal method to check prime number in this question but its giving time limit limit, is there any other fast algorithm?

@sktg99 Use Sieve of Eratosthenes to generate the prime numbers. It is taught in the Number Theory section of your course.