can you look into the code why it is not passing all the test cases
To print all the test cases
#include using namespace std; bool prime(int n) { if(n==2) { return true; } for(int i=2;ii<=n;i++) { if(n%i==0) { return false; } } return true; } int main() { int N; cin>>N; int count = 1; if(N==1) { cout<<“2”; } for(int i=2;i<NN;i++) { if(count>N) { return 0; } if(prime(i)) { cout<<i<<endl; count++; continue; } } }
bdw i am already using it in collaborative mode
@sourabhsingh282
In your code for each input you are just printing 2 as output. This is because in the bool function you are running for loop for i=2 to i=n and when i==0 then it returns false, this is why no other prime number is printed. For example if you make a call to prime(3) then it will return false because when i=3 then n%i==0. So to correct it you need to run loop from i=0 to i<=n/2. And also you are printing all the first n prime numbers using
if(prime(i)) {
cout<<i<<endl;
count++;
continue;
}
Instead of this what you need to do is that initialise count=0 and if i is prime then only just increment count and when count==n then print i. This will pass most of the test cases but you still face TLE in some cases. Try to think of Sieve method to solve this problem, and if you still not able to build the logic then i will share you the code for your better understanding but first give it a try.