Hi
Below is the program to extract the prime numbers from given array as well as you can check the
single number that is prime or not.
function isPrimeNumber($number)
{
if ($number < 2) {
return false;
}
for ($i=2; $i<=($number / 2); $i++) {
if($number % $i == 0) {
return false;
}
}
return true;
}
function checkPrimeNumber($arr)
{
$len = count($arr);
$pArr = array();
for($i=0;$i<$len;$i++) {
$checkPrime = isPrimeNumber($arr[$i]);
if($checkPrime == true)
array_push($pArr,$arr[$i]);
}
return $pArr;
}
Sample :
$arr = array(2,3,8,9,11,13,17,19,21,23,16,15);
$pArr = checkPrimeNumber($arr);
echo '<pre>';
print_r($pArr);
If you want to check that any number is prime or not , you can check by calling function isPrimeNumber($number) .
Below is the program to extract the prime numbers from given array as well as you can check the
single number that is prime or not.
function isPrimeNumber($number)
{
if ($number < 2) {
return false;
}
for ($i=2; $i<=($number / 2); $i++) {
if($number % $i == 0) {
return false;
}
}
return true;
}
function checkPrimeNumber($arr)
{
$len = count($arr);
$pArr = array();
for($i=0;$i<$len;$i++) {
$checkPrime = isPrimeNumber($arr[$i]);
if($checkPrime == true)
array_push($pArr,$arr[$i]);
}
return $pArr;
}
Sample :
$arr = array(2,3,8,9,11,13,17,19,21,23,16,15);
$pArr = checkPrimeNumber($arr);
echo '<pre>';
print_r($pArr);
If you want to check that any number is prime or not , you can check by calling function isPrimeNumber($number) .
No comments:
Post a Comment