Ques:
Prateek recently graduated from college. To celebrate, he went to a candy shop and bought all the candies. The total cost was a number upto which there are n prime numbers (starting from 2). Since Prateek wants to minimize his cost, he calls you to help him find the minimum amount that needs to be paid. Being a student of Prateek bhaiya it is now your job to help him out Input Format First line contains a single integer denoting the number of test cases T. Next T lines contains a single integer N, denoting the number of primes required. Constraints T <= 10000 It is guaranteed that the answer does not exceed 10^6.
My code:
import java.util.;
public class Main {
public static void main(String args[]) {
Scanner scn=new Scanner(System.in);
int t=scn.nextInt();
while(t–>0){
int n=scn.nextInt();
int count=0;
int i=2;
while(count<n){
if(checkprime(i)){
count++;
}
i++;
}
System.out.println(i-1);
}
}
public static boolean checkprime(int n){
for(int i=2;ii<=n;i++){
if(n%i==0){
return false;
}
}
return true;
}
}
Issue: TLE Just for 1 case. May be because of Large input. Can you help me with Use Sieve of Eratosthenes for this problem. Help!