Guest User

Untitled

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