A prime number is an integer greater than 1 with exactly two positive factors: 1 and itself. Values below 2 are therefore not prime. Testing divisors only up to the square root is sufficient.
<?php
function isPrime(int \): bool
{
if (\ < 2) {
return false;
}
for (\ = 2; \ * \ <= \; \++) {
if (\ % \ === 0) {
return false;
}
}
return true;
}
echo isPrime(18) ? 'Prime number' : 'Not a prime number';
?>
\=18;
\=0;
for(\=2;\<=(\/2); \++){
if(\%\ == 0 ){
\=1;
break;
}
}
if(\==0){
echo \"$n1 is a prime number \" ;
}else{
echo \"$n1 is NOT a prime number \" ;
}
Output
18 is NOT a prime number
\2=\Array['n1']; // change this number
echo \"<form method=POST action=''>
<input type=text name=n1 value='$n1'>
<input type=Submit value=submit></form>\";
$flag=0;
for($i=2;$i<=($n1/2); $i++){
if($n1%$i == 0 ){
$flag=1;
break;
}
}
if($flag==0){
echo \"$n1 is a prime number \" ;
}else{
echo \"$n1 is NOT a prime number \" ;
}
for(\=2;\<=100;\++){
\2=\;
\0=0;
for(\2=2;\2<=(\2/2); \2++){
if(\2%\2 == 0 ){
\0=1;
break;
}
}
if(\0==0){
echo \"$n1 , \" ;
}
}
Output is here
2 , 3 , 5 , 7 , 11 , 13 , 17 , 19 , 23 , 29 , 31 , 37 , 41 , 43
, 47 , 53 , 59 , 61 , 67 , 71 , 73 , 79 , 83 , 89 , 97 ,
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.