Horje
PHP gmp_binomial() Function

The gmp_binomial() function is an inbuilt function in PHP that is used to calculate the binomial coefficients. The binomial coefficient, often denoted as “n choose k” or “C(n, k)”, represents the number of ways to choose “k” elements from a set of “n” distinct elements without regard to the order.

Syntax:

gmp_binomial(GMP|int|string $n, int $k): GMP

Parameters: This function accepts two parameters that are described below.

  • $n: This parameter must be an integer.
  • $k: This parameter must be an integer type. If this parameter is negative. It will occur the error.

Return Value: The gmp_binomial() function returns the coefficient of the numbers c(n, k) ;

Program 1: The following program demonstrates the gmp_binomail() function.

PHP

<?php
  
// Assuming you have the GMP extension enabled
$n = 6 ;
$k = 2 ;
$binomialCoefficient = gmp_binomial($n, $k);
  
echo "Binomial Coefficient C($n, $k) is: "
      . $binomialCoefficient . "\n";
?>

Output

Binomial Coefficient C(6, 2) is: 15

Program 2: The following program demonstrates the gmp_binomail() function.

PHP

<?php
  
// Assuming gmp library in enable in your PHP
  
try {
    $n = 5; // Total number of distinct elements
    $k = -2 ;
      
      if($k < 0) {
        throw new Exception("$k value must be greater than 0") ;
    }
    
      $binomialCoefficient = gmp_binomial($n, $k);
        
    echo "Binomial Coefficient C($n, $k) is: "
          . $binomialCoefficient . "\n";
       } catch(Exception $e) {
          echo $e->getMessage() ;
}
  
?>

Output:

k value must be greater than 0 

Reference: https://www.php.net/manual/en/function.gmp-binomial.php




Reffered: https://www.geeksforgeeks.org


PHP

Related
PHP sprintf() Function PHP sprintf() Function
PHP vfprintf() Function PHP vfprintf() Function
PHP token_get_all() Function PHP token_get_all() Function
PHP token_name() Function PHP token_name() Function
PHP mb_strrichr() Function PHP mb_strrichr() Function

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
13