Cover Image for PHP gmp_fact() Function
202 views

PHP gmp_fact() Function

As of my last update in September 2021, PHP’s GMP (GNU Multiple Precision) extension includes the gmp_fact() function. The gmp_fact() function is used to calculate the factorial of a non-negative integer represented as a GMP number.

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

GMP gmp_fact ( mixed $num )

Parameter:

  • $num: The GMP number or numeric string representing the non-negative integer for which you want to calculate the factorial.

Return value:

  • The gmp_fact() function returns a GMP number representing the factorial of the input number.

Example:

$n = 5;

$factorial = gmp_fact($n);

echo "Factorial of $n is: " . gmp_strval($factorial); // Output: 120

In this example, the gmp_fact() function is used to calculate the factorial of the integer 5. The result is 120, as 5! (5 factorial) is equal to 5 x 4 x 3 x 2 x 1 = 120.

The gmp_fact() function is particularly useful when dealing with large factorial calculations, as it can handle arbitrary precision arithmetic, allowing the computation of factorials for large integers that would otherwise overflow in regular integer arithmetic.

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