Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Find the Next Prime Number

Basic Acceptance 100.00% Points 10.00

You are given a positive integer n. Your task is to return the smallest prime number strictly greater than n.

A prime number is an integer greater than 1 that has exactly two distinct positive divisors: 1 and itself. The returned prime must be greater than n, not equal to it.

Examples
Example 1

Input: n = 10
Output: 11
Explanation: The smallest prime number strictly greater than 10 is 11.

Example 2

Input: n = 13
Output: 17
Explanation: The numbers 14, 15, and 16 are not prime, so the next prime after 13 is 17.

Example 3

Input: n = 1
Output: 2
Explanation: The smallest prime number greater than 1 is 2.

Constraints
  • 1 = n = 10¹²
Companies
Microsoft Flipkart
Topics
Data Structures Basics 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