Prime Check
PHP
Medium
5 views
Problem Description
Given n, print PRIME if n is prime else NOT_PRIME.
Input Format
One integer n.
Output Format
PRIME or NOT_PRIME.
Official Solution
<?php
$inputText=trim(stream_get_contents(STDIN));
if($inputText==='') exit;
$n=intval($inputText);
if($n<2){ echo 'NOT_PRIME'; exit; }
if($n%2===0){ echo ($n===2)?'PRIME':'NOT_PRIME'; exit; }
$lim=intval(floor(sqrt($n)));
for($d=3;$d<=$lim;$d+=2){
if($n%$d===0){ echo 'NOT_PRIME'; exit; }
}
echo 'PRIME';
?>
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!