I m getting TLE in a question

in java crux course, prateel loves candy question :
i tried to solve it as follows but getting TLE on submitting while with custom input its working fine …
please help …

CODE -

import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int i,n,p,count,flag;
int amount =0;

	int cases = sc.nextInt();

	for(int j=1; j<=cases; j++){
		n=sc.nextInt();
		p=2;
		i=1; 
		while(i<=n){
		flag=1;
		for(count=2;count<=p-1;count++){
			if(p%count==0){
					flag=0;
					break;
				} 
			}
			if(flag==1){
				amount = p;
				i++;
			}
			p++;
		}
		System.out.println(amount);
	}
}

}

hey @amigarg2001_4d6124603ec7a2bd You are getting TLE because your code is not efficient for higher numbers.
So you have to use SOE approach which is very efficient to store prime numbers and use them.
Either you can search in the classroom contents and Garima mam will explain this to you.
otherwise follow this link : https://www.geeksforgeeks.org/sieve-of-eratosthenes/

hey @amigarg2001_4d6124603ec7a2bd Since there is no reply from you for a while I hope I was able to clear your doubt.So for Now I am marking this doubt as resolved.you can reopen it if you want.

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.