Update appNew update is available. Click here to update.
sidenav-btnClose
Topic list
Find Nth Prime
MEDIUM
15 mins
15 upvotes
Other topics
Problem solved
Badge
Skill meter
Strings
-
-
Matrices (2D Arrays)
-
-
Linked List
-
-
Sorting
-
-
Binary Search
-
-
Stacks & Queues
-
-
Trees
-
-
Graph
-
-
Dynamic Programming
-
-
Greedy
-
-
Tries
-
-
Arrays
-
-
SQL
-
-
Binary Search Trees
-
-
Heap
-
-
Bit Manipulation
-
-
Solve problems & track your progress
Checkout your overall progress in every topic here
Become
userLevel
Sensei
in DSA topics
Open the topic and solve more problems associated with it to improve your skills
Check out the skill meter for every topic
See how many problems you are left with to solve for cracking any stage. Score more than zero to get your progress counted.

Find Nth Prime

Contributed by
Dhruv Sharma
Medium
yellow-spark
0/80
Avg time to solve 15 mins
Success Rate 85 %
Share
15 upvotes

Problem Statement

You are given a number 'N'. Your task is to find Nth prime number.

A prime number is a number greater than 1 that is not a product of two smaller natural numbers. Prime numbers have only two factors – 1 and the number itself.

Any number greater than 0 is a natural number i.e. natural numbers N = {1, 2, 3, 4,....}

For example:-

If you are asked to find the 7th prime number, it’ll be 17 because the first 7 prime numbers are 2, 3, 5, 7, 11, 13, 17.

Note: 1 is not a prime number.

Follow Up:
Try to solve the problem in O(N).
Detailed explanation ( Input/output format, Notes, Constraints, Images )
Sample Input 1:
3
5 
2
1
Sample Output 1:
11
3
2
Explanation for sample input 1:
For the first query, the prime numbers are [2, 3, 5, 7, 11]

For the second query, the prime numbers are [2, 3]

For the third query, the prime number is [2]
Sample Input 2:
3
46
20
13
Sample Output 2:
199
71
41
Reset Code
Full screen
Auto
copy-code
Console