Challenge 8 probleM

The code that I wrote is showing the desired output when I compile but when I submit it shows an error !TLE.
I don’t understand th eproblem.
My code is:

import java.util.*;

public class Main {

public static void main(String args[]) {

	Scanner scn=new Scanner(System.in);
	int n=scn.nextInt();
	int i,j,p,P,temp=0;
//	boolean bool;
	for(i=0;i<n;i++) {
		p=scn.nextInt();
		P=0;
		j=2;
		while(P!=p) {
			int count=0;
			for(int k=1;k<=j;k++) {
				if(j%k==0) {
					count++;
					//System.out.println(+count);
					//System.out.println(+k);
					//System.out.println(+j);
				}
			}
			if(count==2) {
				P++;
			}
			temp=j;
			j++;
		}
		System.out.println(+temp);
	}

}

}

@VinayakSingh11111,

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.

but it is not mentioned to use thw method you are saying.

@VinayakSingh11111,

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.

@VinayakSingh11111,

There will be another question that uses this approach to give you a better understand of the use cases of this algorithm.

but why it is not submitting.The code is correct.

@VinayakSingh11111,

It is giving a TLE, right? That’s because the test cases are large and your approach is not optimized yet.

so i have to use the sieve of eratosthenes to solve it?

@VinayakSingh11111,
Yeah, If you face any problems with the implementation, I am here to help you :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.