i am getting error tle.ihow to write more efficient since till here all i have read has been applied
Getting error tle
Please post your code
import java.util.*; public class Main { public static void main(String args[]) { Scanner input = new Scanner(System.in); int n = input.nextInt(); for (int i = 1; i <= n; i++) { int no = input.nextInt(); int k = 2; int count = 0; while (true) { boolean b = true; for (int prev = 2; prev <= k/2; prev++) { if (k % prev == 0) { b = false;// not prime condition } } if (b) { count++; if (count == no) { System.out.println(k); break; } } k++; } } } }
Use sieve to store the primes beforehand and then simply print the nth prime in O(1)Your solution doesn’t fit the time constraints.
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.