TLE Error is comsing while submitting this code please help!

I’m Submitting below code but i didn’t able to understant why this error come an what is means please help me…

import java.util.*;

public class Main {
public static void main(String args[]) {
Scanner scn = new Scanner(System.in);
int t = scn.nextInt();
int count = 1;
for (int i = 1; i <= t; i++) {

		int a = scn.nextInt();
		

		int n = 2;
		while (count <= a) {

			String check = "true";
			for (int j = 2; j < n; j++) {

				if (n % j == 0) {
					check = "false";
				}
			}

			if (check.equals("true")) {
				count = count + 1;
			}

			n = n + 1;
		}
		System.out.println(n - 1);
	}

}

}

@nigamshubham1998,

You are supposed to use the prime seive optimisation technique (sieve of eratosthenes). Also make sure that you execute 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.

Thank you sir, but I want to tell you that I have to get this type of algorithm first.There is no topic in section 2 of the course, how will I understand this, how is this algorithm. i have no idea related this thing

@nigamshubham1998,

This method is used widely for prime numbers and to reduce the time complexity. It wasn’t in the lectures because this approach is best understood when implemented first hand. Hence the question. The algorithm is as it is explained in my previous post.

@nigamshubham1998,

@nigamshubham1998,
https://ide.codingblocks.com/s/253474 corrected code.

Use an arraylist. Size of prime will be 1000001.

Let me know in case you have any further doubt :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.