GMP gmp_gcdext

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

gmp_gcdext — GCD 및 승수 계산


설명

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

a*s + b*t = g = gcd(a,b)가 되도록 g, s, t를 계산합니다. 여기서 gcd는 최대공약수입니다. 각각의 요소 g, s 및 t가 있는 배열을 반환합니다.

이 함수는 두 변수에서 선형 디오판틴 방정식을 푸는 데 사용할 수 있습니다. 다음은 정수 솔루션만 허용하는 방정식이며 형식은 a*x + b*y = c입니다. 자세한 내용은 MathWorld의 » "Diophantine Equation" 페이지로 이동하세요.


매개변수

num1
GMP 개체, int 또는 숫자 문자열입니다.
num2
GMP 개체, int 또는 숫자 문자열입니다.

반환 값

GMP 번호의 배열입니다.


Examples

예제 #1 선형 디오판틴 방정식 풀기

                  
<?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
?>