Here is my code, My code runs well, but Time limit is exceeded, I don’t know how to reduce the time limit. Can u please help me?
import java.util.Scanner;
public class Main {
public static int solution(int c) {
int num = 3;
int val = 1;
if (c ==1) {
System.out.println(2);
}
else {
for (int i = 2; i <= c;) {
for (int j = 2; j <= Math.sqrt(num); j++) {
if (num % j == 0) {
val = 0;
break;
}
}
if (val != 0) {
i++;
}
val = 1;
num++;
}
System.out.println(num-1);
}
return 0;
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int t = s.nextInt();
int[] a = new int[t];
for (int i = 0; i < t; i++) {
a[i] = s.nextInt();
}
for(int i=0;i<t;i++) {
int b=a[i];
solution(b);
}
}
}