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.
- If
targetis less than 2, print "false". - Write a loop starting at 2 up to the square root of
target(Math.sqrt(target)). - If
targetis divisible by any number in the loop, print "false" and return/exit. - If no divisors are found after the loop, print "true".
01EXAMPLE 1
Input
target = 7Output
trueExplanation: 7 is only divisible by 1 and 7.
02EXAMPLE 2
Input
target = 4Output
falseExplanation: 4 is divisible by 2.
Constraints
- Use Math.sqrt() for efficiency.
- Handle numbers < 2 correctly.
MathLoopsLogic
JavaScriptSystem handles I/O — write your function only
Loading...
3 Hidden
Input Arguments
target7
Expected Output
true
Click RUN to test your solution