How to rectify time limit problem?

Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
if(N<=10000) {
while(N!=0) {
int T = sc.nextInt();
int num = 3;
int flag = 0;
int i=2;
int result = 0;
if(T>=1) {
result = i;
}
for(;i<=T;) {
for(int j=2;j<=Math.sqrt(num);j++) {
if(num%j==0) {
flag = 1;
break;
}
}
if(flag==0) {
result = num;
i++;
}
flag=0;
num++;
}
System.out.println(result);
N–;
}
}

Hey @prateekad99,

In this question you need to use the sieve technique to deal with the test cases.

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.

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.