Naxocist

Nth Prime

Apr 26th, 2022
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.49 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define ll long long
  6.  
  7. using pi = pair<int, int>;
  8.  
  9. const int N = 8e6 + 3;
  10. bool notprime[N];
  11. vector<int> prime;
  12.  
  13. int main() {
  14.     ios::sync_with_stdio(false); cin.tie(nullptr);
  15.    
  16.     int n; cin >> n;
  17.     int cnt=0;
  18.    
  19.  
  20.     for(int i=2; i*i < N; ++i){
  21.         if(!notprime[N]){
  22.            
  23.             for(int j=i*i; j<N; j+=i) notprime[j] = true;
  24.         }
  25.  
  26.     }
  27.     for(int i=2; i<=N; ++i) if(!notprime[i]) prime.push_back(i);
  28.  
  29.     cout << prime[n-1];
  30.     return 0;
  31.  
  32. }
  33.  
Advertisement
Add Comment
Please, Sign In to add comment