oneby7

Foundations Working with digits and divisors

34. Check Whether a Number Is Prime

Medium

Given a positive integer n, return true if n is prime, and false otherwise.

A number is prime when it is greater than 1 and its only divisors are 1 and itself. So 2, 3, 5 and 7 are prime. 6 is not, because 2 and 3 divide it. 1 is not prime either.

Example 1

Input: n = 91

Output: false

Explanation: 91 = 7 × 13, so 7 and 13 divide it as well as 1 and 91.

Example 2

Input: n = 1

Output: false

Explanation: A prime must be greater than 1.

Example 3

Input: n = 2147483647

Output: true

Explanation: No number from 2 to 2,147,483,646 divides it, so its only divisors are 1 and itself.

Check your understanding

Work out the answer for this input before you write any code.

n = 2

Constraints

  • 1 ≤ n ≤ 1010

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Article

Send feedback

Thank you for helping. We read every report and fix what is wrong.

How do you feel about oneby7?

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.