Saturday, August 30, 2025
HomeLanguagesPHP gmp_binomial() Function

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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32250 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6617 POSTS0 COMMENTS
Nicole Veronica
11792 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11840 POSTS0 COMMENTS
Shaida Kate Naidoo
6733 POSTS0 COMMENTS
Ted Musemwa
7014 POSTS0 COMMENTS
Thapelo Manthata
6689 POSTS0 COMMENTS
Umr Jansen
6704 POSTS0 COMMENTS