Find the nth prime

#include<bits/stdc++.h>
using namespace std;
bool isPrime(int n)
{
// Corner cases
if (n <= 1)
return false;
if (n <= 3)
return true;

// This is checked so that we can skip 
// middle five numbers in below loop 
if (n % 2 == 0 || n % 3 == 0) 
    return false; 

for (int i = 5; i * i <= n; i = i + 6) 
    if (n % i == 0 || n % (i + 2) == 0) 
        return false; 

return true; 

}
int main()
{
int t;
cin >> t;
while (t–)
{
int count;
cin>>count;
for (int i = 2; i <= INT_MAX;i++){
if(isPrime(i))
count–;
if(count==0)
{
cout << i << “\n”;
break;
}
}
}
return 0;
}

this is my code its not working for a single test case showing time limit error is there any other efficient way

https://ide.codingblocks.com/s/277124 heres the link

@tharunnayak14 using basic brute approches to find prime will give you TLE here. use Sieve of Eratosthenes for this type of problems.

2 Likes

what is that?
IDK what u meant

then i suggest you to study Sieve of Eratosthenes which should be in your course, or from any other source convenient to you. it is very simple and efficient method to handle prime number problems.

2 Likes

ok ill do that thanks for suggestion

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