PHP - Fonction gmp_gcdext ()

Définition et utilisation

le gmp_gcdext() La fonction calcule le GCD et les multiplicateurs pour les nombres donnés.

La description

gmp_gcdext () aide à résoudre des équations diophantiennes linéaires à deux variables par exemple: a * s + b * t = g = gcd (a, b) .Il renvoie un tableau avec les valeurs g, s et t.

Syntaxe

gmp_gcdext ( GMP $a , GMP $b ) : array

Paramètres

Sr. Non Paramètre et description
1

a

Il peut s'agir d'un numéro de ressource GMP, d'un objet gmp ou d'une chaîne numérique.

2

b

Il peut s'agir d'un numéro de ressource GMP, d'un objet gmp ou d'une chaîne numérique.

Valeurs de retour

La fonction PHP gmp_gcdext () renvoie un tableau de nombres GMP.

Version PHP

Cette fonction fonctionnera à partir de la version PHP supérieure à 5.0.0.

Exemple 1

Fonctionnement de gmp_gcdext -

<?php
   // Solve the Diophantine  equation a*s + b*t = g = gcd(a,b)
   // where a = 18, b = 24, g = gcd(18, 24) = 6
   $a = gmp_init(18);
   $b = gmp_init(24);
   $g = gmp_gcd($a, $b);
   $r = gmp_gcdext($a, $b);

   $check_gcd = (gmp_strval($g) == gmp_strval($r['g']));
   $eq_res = gmp_add(gmp_mul($a, $r['s']), gmp_mul($b, $r['t']));
   $check_res = (gmp_strval($g) == gmp_strval($eq_res));

   if ($check_gcd && $check_res) {
      $fmt = "The result of the equation is : %d*%d + %d*%d = %d\n";
      printf($fmt, gmp_strval($a), gmp_strval($r['s']), gmp_strval($b),
      gmp_strval($r['t']), gmp_strval($r['g']));
   } else {
      echo "Error";
   }   
?>

Cela produira le résultat suivant -

The result of the equation is : 18*-1 + 24*1 = 6

Exemple 2

Fonctionnement de gmp_gcdext -

<?php
   // Solve the Diophantine  equation a*s + b*t = g = gcd(a,b)
   // where a = 24, b = 36, g = gcd(24, 36) = 12
   $a = gmp_init(24);
   $b = gmp_init(36);
   $g = gmp_gcd($a, $b);
   $r = gmp_gcdext($a, $b);

   $check_gcd = (gmp_strval($g) ==  gmp_strval($r['g']));
   $eq_res = gmp_add(gmp_mul($a, $r['s']), gmp_mul($b, $r['t']));
   $check_res = (gmp_strval($g) == gmp_strval($eq_res));

   if ($check_gcd && $check_res) {
      $fmt = "The result of the equation is : %d*%d + %d*%d = %d\n";
      printf($fmt, gmp_strval($a), gmp_strval($r['s']), gmp_strval($b),
      gmp_strval($r['t']), gmp_strval($r['g']));
   } else {
      echo "Error";
   }
?>

Cela produira le résultat suivant -

The result of the equation is : 24*-1 + 36*1 = 12