What is the error? this is giving TLE

#include
using namespace std;

int isPrimeNumber(int);

int main()
{
bool isPrime; int t;
cin>>t;
int c;
while(t–){
cin>>c;

int a[10000];
int k=0;
for(int n = 2; n <10000; n++) {
// isPrime will be true for prime numbers
isPrime = isPrimeNumber(n);

  if(isPrime == true)
  {  a[k]= n;
    k++;}

}

 cout<<"\n";

cout<<a[c-1];

}

return 0;
}

// Function that checks whether n is prime or not
int isPrimeNumber(int n) {
bool isPrime = true;

for(int i = 2; i <= n/2; i++) {
if (n%i == 0) {
isPrime = false;
break;
}
}
return isPrime;
}

hello @Aditikumari
your logic is correct but time complexity is high.

to optimise it use sieve and store all prime numbers upto 10^6 in some array(say prime) .
and then print nth prime number using ur array (i.e print prime[n-1] )

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.

1 Like