gcd( $number, $modulus ); if ( (int)$r[2] === 1 ) { $a = $r[0]; while ( bccomp( $a, 0 ) < 0 ) { $a = bcadd( $a, $modulus ); } return $a; } else { return false; } } /** * Finds the greatest common denominator of two numbers using the extended * Euclidean algorithm. * * The returned array is ( x, y, gcd( a, b ) ), where * x * a + y * b = gcd( a, b ) * * @param string $a The first number * @param string $b The second number * @return array(string) */ public function gcd( $a, $b ) { $x = 0; $xLast = 1; $y = 1; $yLast = 0; while ( bccomp( $b, 0 ) !== 0 ) { $temp = $b; $q = bcdiv( $a, $b, 0 ); $b = bcmod( $a, $b ); $a = $temp; $temp = $x; $x = bcsub( $xLast, bcmul( $q, $x ) ); $xLast = $temp; $temp = $y; $y = bcsub( $yLast, bcmul( $q, $y ) ); $yLast = $temp; } return array( $xLast, $yLast, $a ); } /** * Compares two numbers. * * Returns an integer: * - a positive value if $a > $b * - zero if $a == $b * - a negative value if $a < $b * * @param string $a The first number * @param string $b The second number * @return int */ public function cmp( $a, $b ) { return bccomp( $a, $b ); } /** * Returns the string representation of number $a. * * @param string $number The number to be represented as a string * @return string */ public function toString( $number ) { return $number; } } ?>