gmp_invert


gmp apg

COMPUTES the inverse of a GMP number by a given modulo.





This function returns FALSE if the inverse does not exist.

On the obtained values we can write gmp_mod(gmp_invert($num1, $num2) * $num1, $num2) = 1.



<?php

GMP
|false gmp_invert ( GMP|int|string $num1, 
                                     
GMP|int|string $num2 )

where,

$num1 = The number to invert

$num2 
= The modulo to be used

?>
 

$num1


The given GMP number to obtain the inverse.



$num2


The given GMP number to be used as modulo.



  1 EXERCISE   

<?php

// Run this code several times

$num01a = mt_rand(0, 100);

$num01b = mt_rand(0, 100);
// You must try other values for each mt_rand

echo "num01a = $num01a<br><br>num01b = $num01b<br><br>";

$in01 = gmp_invert($num01a, $num01b);

var_dump($in01);

echo 
'<br><br>';

echo 
"gmp_invert($num01a, $num01b) = " . gmp_strval($in01);

?> 

  2 EXERCISE   

<?php

$arr02 
= [ 55, 64, '021', 21 ]; 

$mod02 = [ 2, 4, 11, 24, 111 ];
// You must try other values for each array

foreach($arr02 as $a02)
{
    foreach(
$mod02 as $m02)
    {
       
$in02 = gmp_invert($a02, $m02);

       
var_dump($in02);

       echo 
'<br>gmp_invert( ' . $a02 . ', ' . $m02 . ' ) = ' . 
                                                
gmp_strval($in02) . 
                                                
'<br>where<br>';
       echo 
"gmp_mod(gmp_invert($a02, $m02) * $a02, $m02) = "; 
       echo 
gmp_mod(gmp_invert($a02, $m02) * $a02, $m02) . '<br><br>';
    }
}

?> 

  3 EXERCISE   

<?php

$a03 
= 55;

$m03 = 111;

// You must try other values

$in03 = gmp_invert($a03, $m03);

$rs03 = gmp_strval($in03);

echo 
'gmp_invert( ' . $a03 . ', ' . $m03 . ' ) = ' . $rs03 . '<br><br>';

echo 
'where:<br><br>';

$cp03 = gmp_mod($rs03 * $a03, $m03);

echo 
'gmp_mod( ' . $rs03 . '*' . $a03 . ', ' . $m03 . ' ) = ' . $cp03;

?>