Calculate GCD and multipliers

# gmp_gcdext

(PHP 4 >= 4.0.4, PHP 5, PHP 7, PHP 8)

gmp_gcdext — Calculate GCD and multipliers

### 说明

function gmp_gcdext(GMP|int|string `$num1`, GMP|int|string `$num2`): array

Calculates g, s, and t, such that `a*s + b*t = g = gcd(a,b)`, where gcd is the greatest common divisor. Returns an array with respective elements g, s and t.

This function can be used to solve linear Diophantine equations in two variables. These are equations that allow only integer solutions and have the form: `a*x + b*y = c`. For more information, go to the » "Diophantine Equation" page at MathWorld

### 参数

`num1`
  GMP 对象、int 或 string，可以按照跟在 gmp_init() 中使用字符串并自动检测 base（即当 base 等于 0 时）相同的逻辑将其解释为数字。
`num2`
  GMP 对象、int 或 string，可以按照跟在 gmp_init() 中使用字符串并自动检测 base（即当 base 等于 0 时）相同的逻辑将其解释为数字。

### 返回值

An array of GMP numbers.

### 示例

示例 #1 Solving a linear Diophantine equation

    <?php
    // Solve the equation a*s + b*t = g
    // where a = 12, b = 21, g = gcd(12, 21) = 3
    $a = gmp_init(12);
    $b = gmp_init(21);
    $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 = "Solution: %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 while solving the equation\n";
    }

    // output: Solution: 12*2 + 21*-1 = 3
    ?>

`↑` and `↓` to navigate • `Enter` to select • `Esc` to close • `/` to open