HACKER RANK Project Euler 07 - 10001st Prime Number - Solution.'C++'

Project Euler Challenges-07 - 10001st Prime Number Solution
Problem Statement
This problem is a programming version of Problem 7 from projecteuler.net
By listing the first six prime numbers: 2,3,5,7,11 and 13, we can see that the 6th prime is 13
What is the N'th prime number?
Input Format 
First line contains T that denotes the number of test cases. This is followed by T lines, each containing an integer, N.
Output Format 
Print the required answer for each test case.
Constraints 
1T103 
1N104
Sample Input
2
3
6
Sample Output
5
13

Solution:

    #include <iostream>
    #include <vector>
    using namespace std;
    const int N=1000000;
    bool mark[N+100];
    int main()
    {
     for(int i=2;i<=N/2;++i)
      if(mark[i]==false)
      {
       int b=i+i;
       while(b<=N)
       {
        mark[b]=true;
        b+=i;
       }
      }
     vector<int> primes;
     for(int i=2;i<=N;++i)
      if(mark[i]==false)
       primes.push_back(i);
     int t;
     cin>>t;
     for(int k=1;k<=t;++k)
     {
      int n;
      cin>>n;
      cout<<primes[n-1]<<endl;
     }
     return 0;
    }

Thanks for Visiting, Hope this helps you....

Comments

Popular Posts