gmp_gcd


gmp apg

CALCULATE the GCD, (Greatest Commom Divisor), between two GMP values.

Euclidean algorithm - From Wikipedia  

Euclid's Algorithm to find GCD of two Numbers  





This function returns always a positive value.

It doesn't matter if $num1 < 0 or $num2 < 0.



<?php

GMP gmp_gcd 
( GMP|int|string $num1, 
                         
GMP|int|string $num2 )

where,

$num1 = The first value to get the GCD

$num2 
= The second value to get the GCD

?>
 

$num1


The first GMP number to get the GCD.



$num2


The second GMP number to get the GCD.



  1 EXERCISE   

<?php

// Rn this code several times

$a = gmp_random_range(PHP_INT_MIN, PHP_INT_MAX);

$b = gmp_random_range(PHP_INT_MIN, PHP_INT_MAX);

$gcd01 = gmp_gcd($a, $b);

echo 
'gmp_gcd( ' . $a .', ' . $b . ' ) = ' . $gcd01 . '<br><br>';

?> 

  2 EXERCISE   

<?php

$arr02 
= [ 'a' => 234, 'b' => 12387, 
                 
'c' => 0, 'd' => 12387, 
                 
'e' => 224, 'f' => 0, 
                 
'g' => -1, 'h' => 0, 
                 
'i' => "12371238123", 'j' => "32618723123", 
                 
'k' => "7623456735", 'l' => "12372341234" ];

function 
gcdgmp ($v01, $v02)
{
    echo 
"gmp_gcd($v01, $v02) = " . 
             
gmp_gcd($v01, $v02) . '<br><br>';
}

gcdgmp ($arr02['a'], $arr02['b']);
    
gcdgmp ($arr02['c'], $arr02['d']);

gcdgmp ($arr02['e'], $arr02['f']);

gcdgmp ($arr02['g'], $arr02['h']);

gcdgmp ($arr02['i'], $arr02['j']);

gcdgmp ($arr02['k'], $arr02['l']);
echo 
'<br><br>';

$m = gmp_init("8127346234");
var_dump(gmp_strval(gmp_gcd($m,"12372341234")));
echo 
'<br><br>';

$n = gmp_init("8127346234");
var_dump(gmp_strval(gmp_gcd($n,"12372341234")));
echo 
'<br><br>';

$o = gmp_init("8127346234");
var_dump(gmp_strval(gmp_gcd("7623456735",$o)));
echo 
'<br><br>';

$p = gmp_init("8127346234");
var_dump(gmp_strval(gmp_gcd($p,$p)));
echo 
'<br><br>';

$q = gmp_init("8127346234");
var_dump(gmp_strval(gmp_gcd($q,0)));

?>