PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

gmp_hamdist> <gmp_gcd
Last updated: Fri, 22 Aug 2008

view this page in

gmp_gcdext

(PHP 4 >= 4.0.4, PHP 5)

gmp_gcdextCalcula el MCD y los coeficientes

Descripción

array gmp_gcdext ( resource $a , resource $b )

Calcula g, s y t, tales que cumplen que a*s + b*t = g = mcd(a,b), donde mcd es el máximo común divisor. Devuelve una matriz con los elementos g, s y t.

La función se puede emplear para resolver ecuaciones Diofánticas lineales con 2 variables. Este tipo de ecuaciones solamente permiten soluciones enteras y son de la forma: a*x + b*y = c. Para más información, puede visitar la dirección » "Diophantine Equation" de MathWorld

Example #1 Resolución de una ecuación Diofántica lineal

<?php
// Resolver la ecuacion a*s + b*t = g
// donde a = 12, b = 21, g = mcd(12, 21) = 3
$a gmp_init(12);
$b gmp_init(21);
$g gmp_gcd($a$b);
$r gmp_gcdext($a$b);

$comprueba_mcd = (gmp_strval($g) == gmp_strval($r['g']));
$resultado_ecuacion gmp_add(gmp_mul($a$r['s']), gmp_mul($b$r['t']));
$comprueba_resultado = (gmp_strval($g) == gmp_strval($eq_res));

if (
$comprueba_mcd && $comprueba_resultado) {
    
$fmt "Solucion: %d*%d + %d*%d = %d\n";
    
printf($fmtgmp_strval($a), gmp_strval($r['s']), gmp_strval($b),
    
gmp_strval($r['t']), gmp_strval($r['g']));
} else {
    echo 
"Error al intentar resolver la ecuacion\n";
}
    
// Salida:  Solution: 12*2 + 21*-1 = 3
?>



add a note add a note User Contributed Notes
gmp_gcdext
FatPhil
15-Jun-2003 06:47
The extended GCD can be used to calculate mutual modular inverses of two
coprime numbers. Internally gmp_invert uses this extended GCD routine,
but effectively throws away one of the inverses.

If gcd(a,b)=1, then r.a+s.b=1
Therefore  r.a == 1 (mod s) and s.b == 1 (mod r)
Note that one of r and s will be negative, and so you'll want to
canonicalise it.

gmp_hamdist> <gmp_gcd
Last updated: Fri, 22 Aug 2008
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites