Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Check Whether a Number Is Prime Using vN Optimization

Basic Acceptance 99.99% Points 10.00

You are given an integer n. Your task is to determine whether it is a prime number by checking divisibility only up to the square root of n.

A prime number is a positive integer greater than 1 that has exactly two distinct positive divisors: 1 and itself. Return Yes if n is prime; otherwise, return No.

Examples
Example 1

Input: n = 29
Output: Yes
Explanation: No integer from 2 through √29 divides 29 evenly, so 29 is prime.

Example 2

Input: n = 100
Output: No
Explanation: The number 100 is divisible by 2, so it has a divisor other than 1 and itself and is not prime.

Example 3

Input: n = 1
Output: No
Explanation: The number 1 is not prime because it has only one positive divisor.

Constraints
  • 1 = n = 10¹²
Companies
BNY Mellon
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