What is the mistake in this code...? i'm TLE for this code

import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
while(T>0){
int tn = sc.nextInt();
int count = 0;
int number=0;

	int n = 1;
	while(count<tn) {
		for (int i = 1; i*i <= tn; i++) {
			boolean flag = true;
			for (int div = 2; div <= n - 1; div++) {
				if (n % div == 0) {
					flag = false;

				}

			}
			if (n < 2) {
				flag = false;
			}

			if (flag == true) {
				
				
				//System.out.println(count + "count");
				

				//System.out.println(n);
				
				
				
				count++;
				
				if(count==tn) {
					System.out.println(n);
				}
				
				if(count==tn) break;
				
				
				
				

			}
			

			
			
			
			

			n++;

		}
		
		
	

		
	}

		T--;
	}

	

}

}

@LakshmiPrasanna0303,
You are supposed to use the prime seive optimisation technique. Also make sure that you call the primeseive function that you will make before the test cases only to prevent TLE.

Precompute the prime numbers using sieve technique.

In the 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.

When you finish the loop, count all the positions marked as true and when count is equal to the given test case value print that index. This will be our final answer.

Brute force is something relying on or achieved through the application of force, effort, or power in usually large amounts instead of more efficient methods. Your method is brute force because you are checking for every number instead of using an efficient algorithm.

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.