The Prime Sentinel

MediumAcc. 89.5%
+25 XP 10

The Indivisibles

A Prime Number is a natural number greater than 1 that has no positive divisors other than 1 and itself.

The Assignment

Your function receives a parameter named target.

  1. If target is less than 2, print "false".
  2. Write a loop starting at 2 up to the square root of target (Math.sqrt(target)).
  3. If target is divisible by any number in the loop, print "false" and return/exit.
  4. If no divisors are found after the loop, print "true".

01EXAMPLE 1

Inputtarget = 7
Outputtrue

Explanation: 7 is only divisible by 1 and 7.

02EXAMPLE 2

Inputtarget = 4
Outputfalse

Explanation: 4 is divisible by 2.

Constraints

  • Use Math.sqrt() for efficiency.
  • Handle numbers < 2 correctly.
MathLoopsLogic
JavaScript
Loading...
3 Hidden

Input Arguments

target7

Expected Output

true

Click RUN to test your solution