'Find smallest prime number in array
How can I find the smallest prime number in given array? In my given array, how can I return the 7? I used the array sort method. Please help me. Thanks in advance!
<?php
$array = array(7, 4, 11, 6, 13, 11, 17, 8, 10, 13, 7);
sort($array);
$isPrime = 0;
$count = 0;
$lowestNumber = null;
function isJeffian($num) {
if($num < 2) {
return 0;
}
for($i = 2; $i <= $num / 2; $i++) {
if ($num % $i == 0) {
return 0;
}
}
return 1;
}
for($i = 0; $i < sizeof($array); $i++) {
$isPrime = isJeffian($array[$i]);
if($lowestNumber == null) {
$lowestNumber = $isPrime;
}
if($isPrime == 1) {
$count++;
}
}
echo "Count of number of primes in array : " . $count . "<br/>";
echo "Smallest Prime Number: " . $lowestNumber;
?>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
