Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Find the Nth Prime Number

Basic Acceptance 84.00% Points 10.00

You are given a positive integer n. Your task is to return the nth prime number, counting prime numbers in ascending order starting from 2.

A prime number is an integer greater than 1 that has exactly two distinct positive divisors: 1 and itself.

Examples
Example 1

Input: n = 1
Output: 2
Explanation: The first prime number is 2.

Example 2

Input: n = 5
Output: 11
Explanation: The first five prime numbers are 2, 3, 5, 7, and 11. Therefore, the fifth prime is 11.

Example 3

Input: n = 10
Output: 29
Explanation: The tenth prime number in ascending order is 29.

Constraints
  • 1 = n = 105
Companies
Meta ServiceNow
Topics
Data Structures Basics Searching Algorithm Basics
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions