Prime Check

Prime Check

Medium PHP PHP Control Flow 22 views
Explanation Complexity

Problem Statement

Given n, print PRIME if n is prime else NOT_PRIME.

Input Format

One integer n.

Output Format

PRIME or NOT_PRIME.

Example

29
PRIME

Constraints

0

Input / Output Format

Input Format
One integer n.
Output Format
PRIME or NOT_PRIME.
Constraints
0

Examples

Input:
29
Output:
PRIME

Example Solution (Public)

PHP
<?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';
?>

Official Solution Code

<?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';
?>
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.