Guest User

Untitled

a guest
Dec 24th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.65 KB | None | 0 0
  1.  
  2. #include<bits/stdc++.h>
  3. #include<iostream>
  4. using namespace std;
  5.  
  6. int sieve(int n){
  7.     int a[1000007];
  8.     for(int i = 2; i < 1000000; i++){
  9.         a[i] = 1;
  10.        // cout<<a[i]<<endl;
  11.     }
  12. //  a[0] = 0;
  13. //  a[1] = 0;
  14. //  cout<<a[1]<<a[0]<<a[100]<<endl;
  15.     for(int i = 2; i * i <= 1000000; i++){
  16.        
  17.         if(a[i] == 1){
  18.             for(int j = i * i; j < 1000000; j +=i ){
  19.                 a[j] = 0;
  20.             }
  21.         }
  22.     }
  23.     int count = 0;
  24.     for(int i = 0; i <= 1000000; i++)
  25.     {
  26.             if(a[i] == 1)
  27.                 count++;
  28.            
  29.             if(count == n)
  30.                     return i;
  31.        
  32.     }
  33.  
  34.     return -1;
  35. }
  36.  
  37. int main(){
  38.     int t;
  39.     cin>>t;
  40.     while(t--){
  41.         int n;
  42.         cin>>n;
  43.         cout<<sieve(n)<<endl;
  44.     }
  45.     return 0;
  46. }
Add Comment
Please, Sign In to add comment