gmp_prob_prime


gmp apg

CHECK if a number is probably prime using the Miller-Rabin Probabilistic Primality test.

Miller–Rabin primality test - From Wikipedia  





$repetions ∈ [ 5, 10 ], where 10 is the default.

A higher value lowers the probability for a non-prime to pass as a probable prime.

If this function returns 0, ZERO, $num is not prime.

If this function returns 1, ONE, $num is "probably" prime.

If this function returns 2, TWO, $num is surely prime.


Remembering that:

A prime number is any integer which is divisible exactly by itself and by 1.

A prime number is an positive integer that has no integer factors except one and itself.

A prime number is any integer other than 0 or ± 1 that is not divisible without remainder by any other integers except ± 1 and ± the integer itself.



<?php

int gmp_prob_prime 
GMP|int|string $num
                                                    
int $repetitions 10 )

where,

$num The GMP number to check

$repetions 
The reasonable value

?>
 

$num


The number to be checked if is a prime.



$repetions


The reasonable value.



  1 EXERCISE   

<?php

$arrt01 
= [ 283127777771PHP_INT_MAX ];

foreach(
$arrt01 as $ar01)
{
    
$rs01 gmp_prob_prime($ar0110);
    
    if(
$rs01 == 0)
    {
        echo 
$ar01 ' is NOT a prime!<br><br>';
    }
    elseif(
$rs01 == 1)
    {
        echo 
$ar01 ' is PROBABLY a prime!<br><br>';
    }
    else
    {
        echo 
$ar01 ' is SURELY a prime!<br><br>';
    }
}

?> 

  2 EXERCISE   

<?php

$tst02 
807;

for(
$x02 780$x02 <= $tst02$x02++)
    
// You must try other values
{
    
$t02 gmp_prob_prime($x0210);
    
    if(
$t02 == 2)
    {
        echo 
$x02 ' is SURELY prime!<br><br>';
    }
    elseif(
$t02 == 1)
    {
        echo 
$x02 ' is PROBABLY prime!<br><br>';
    }
    else
    {
        echo 
$x02 ' is NOT prime!<br><br>';
    }
}

?> 

  3 EXERCISE   

<?php

// Try this code several times

$arr03 = [ 10"7"17, -31
                               
"172368715471481723"
                               
"23476812735411"
                               
"19481923" ];

function 
checkprime($a03)
{   
    
$r03 mt_rand(510);
    
    if (
gmp_prob_prime($a03$r03) == 0)
    {
        echo 
"$a03 is NOT a prime ($r03)<br><br>";
    }
    elseif (
gmp_prob_prime($a03$r03) == 1)
    {
        echo 
"$a03 is PROBABLY a prime ($r03)<br><br>";
    }
    else
    {
        echo 
"$a03 is a prime ($r03)<br><br>";
    }
}

foreach(
$arr03 as $k03 => $v03)
{
    
checkprime($v03);
}
?>

  4 EXERCISE   

<?php

$v04a 
10;

for (
$i = -1$i 12$i++)
// You must try other values    
{
    
$n04 gmp_prob_prime(($v04a*$i)-($i*7)-1$i);
    echo (
$v04a*$i)-($i*7)-' => ( ' $n04 ' )';
    
    if (
$n04 == 0)
    {
        echo 
" is NOT a prime";
    }
    elseif (
$n04 == 1)
    {
        echo 
" is PROBABLY a prime";
    }
    else
    {
        echo 
" is a prime";
    } 
    
    echo 
'<br><br>';
}

?>