I am getting time limit exceeded error in my code. I am not able to understand the exact issue here. Please help me.
Question -
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.
Output Format
Print the minimum cost that needs to be paid.
Sample Input
2 5 1
Sample Output
11 2
I have put on my code below.
CODE -
import java.util.*;
public class Main {
static int Prime(int a1)
{
int Prime = 2;
for(int k = 1; k <= a1; )
{
int flag = 1;
for(int j = 2; j <= Prime - 1; j++)
{
if(Prime % j == 0)
{
flag = 0;
break;
}
}
if(flag == 1)
{
k++;
}
Prime++;
}
return Prime-1;
}
public static void main(String args[]) {
Scanner s = new Scanner(System.in);
int n = s.nextInt();
for(int i = 1; i <= n; i++)
{
int a = s.nextInt();
int ans = Prime(a);
System.out.println(ans);
}
}
}