2020-08-09 00:33:43 +05:30
|
|
|
<?php
|
2023-10-04 21:36:11 +03:30
|
|
|
|
2020-08-14 00:20:28 +05:30
|
|
|
/**
|
|
|
|
* This function check whether
|
|
|
|
* the provided integer is a prime
|
|
|
|
* number or not.
|
|
|
|
*
|
|
|
|
* @param Integer $number An integer input
|
|
|
|
* @return boolean whether the number is prime or not
|
|
|
|
*/
|
2020-08-09 00:33:43 +05:30
|
|
|
function isPrime(int $number)
|
|
|
|
{
|
|
|
|
if ($number === 2) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($number % 2 === 0 or $number < 2) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$i = 3;
|
2021-01-04 20:51:24 +01:00
|
|
|
while ($i <= sqrt($number)) {
|
2020-08-09 00:33:43 +05:30
|
|
|
if ($number % $i === 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$i += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|