VOOZH about

URL: https://www.geeksforgeeks.org/php/php-gmp_gcdext-function/

⇱ PHP | gmp_gcdext() function - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

PHP | gmp_gcdext() function

Last Updated : 11 Jul, 2025
The gmp_gcdext() is an inbuilt function in PHP which calculates the GCD ( Greatest Common Divisor ) and multipliers of a given equation such that a * x + b * y = GCD(a, b), where GCD is the greatest common divisor. This function is used to solve linear Diophantine equation in two variables . Syntax:
array gmp_gcdext ( GMP $a, GMP $b )
Parameters: The gmp_gcdext() function accepts two parameters as listed above and described below:
  • $a: This parameter can be a GMP resource in PHP 5.5 and earlier, a GMP object in PHP 5.6, or we are also allowed to pass a numeric string provided that it is possible to convert that string to a number.
  • $b: This parameter can be a GMP resource in PHP 5.5 and earlier, a GMP object in PHP 5.6, or we are also allowed to pass a numeric string provided that it is possible to convert that string to a number.
Return Values: This function will return an array of GMP numbers (GNU Multiple Precision: For large numbers) that is the multipliers (x and y of a given equation ) and the gcd. Examples:
Input: a = 12 , b = 21
 equation = 12 * x + 21 * y = 3
Output: 

Input: a = 5 , b = 10
 equation = 5 * x + 10 * y = 5
Output: x = 1 , y = 0 , GCD(12,21) = 5
Below program illustrate the gmp_gcdext() function: Output:
Solution: 5 * -1 + 6 * 1 = 1
Reference : https://www.php.net/manual/en/function.gmp-gcdext.php
Comment
Article Tags: