I need to find the highest prime number in a given range.
Here is my code which works for 0-100 but if I give 0-125 it is showing prime number as 125.
<
thanks for your reply Samuel Cook ..but i need without using 'gmp_nextprime()' function.. iamgetting message 'Call to undefined function gmp_intval() in C:\wamp\www\highprime.php on line 5' – user1659450 16 mins ago
Since you are having difficultly getting gmp functions
to work then you can use
Example 1 : None GMP Function
$range = range(125, 0); // create the range
foreach ( $range as $v ) {
if (isPrime($v)) {
printf('highest prime number is %d', $v);
break;
}
}
If you are able to get gmp working then you can use gmp_prob_prime
Function Used
Example 1 : Using gmp function
foreach ( $range as $v ) {
if (gmp_prob_prime($v) == 2) {
printf('highest prime number is %d', $v);
break;
}
}
Output
highest prime number is 113
Functions Used
function isPrime($num) {
if ($num == 1)
return false;
if ($num == 2)
return true;
if ($num % 2 == 0) {
return false;
}
for($i = 3; $i <= ceil(sqrt($num)); $i = $i + 2) {
if ($num % $i == 0)
return false;
}
return true;
}