Cover Image for PHP gmp_rootrem() function
196 views

PHP gmp_rootrem() function

As of my last update in September 2021, the gmp_rootrem() function is available in PHP’s GMP (GNU Multiple Precision) extension. The gmp_rootrem() function is used to calculate both the integer nth root and the remainder of a GMP number.

Here’s the syntax of the gmp_rootrem() function:

array gmp_rootrem ( GMP $num , int $nth )

Parameters:

  • $num: The GMP number for which you want to calculate the nth root and remainder.
  • $nth: The positive integer representing the root you want to calculate.

Return value:

  • The gmp_rootrem() function returns an array containing two elements:
  1. The integer nth root of the input GMP number (GMP).
  2. The remainder after raising the integer nth root to the power of $nth (GMP).

Example:

$number = gmp_init('81');
$n = 2; // Calculate square root

list($squareRoot, $remainder) = gmp_rootrem($number, $n);

echo "Square Root: " . gmp_strval($squareRoot) . PHP_EOL; // Output: 9
echo "Remainder: " . gmp_strval($remainder) . PHP_EOL; // Output: 0

In this example, the gmp_rootrem() function is used to calculate the square root (nth root with $n = 2) of the GMP number 81. The resulting square root is 9, and the remainder after raising 9 to the power of 2 is 0.

The gmp_rootrem() function is useful when you need both the integer nth root and the remainder simultaneously, as it avoids redundant calculations.

Please note that the GMP extension needs to be enabled in your PHP configuration to use this function. Additionally, the availability of functions may change in newer PHP versions, so always refer to the PHP manual for the specific version you are using.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS