What is the most efficient way to calculate the binomial coefficient "n over k" in PHP?

To calculate the binomial coefficient "n over k" efficiently in PHP, you can use the formula n! / (k! * (n-k)!). This formula calculates the number of ways to choose k elements from a set of n elements without regard to order. By calculating the factorials of n, k, and n-k separately, you can avoid unnecessary computations and improve efficiency.

function binomialCoefficient($n, $k) {
    $numerator = 1;
    $denominator = 1;
    
    for ($i = 1; $i <= $k; $i++) {
        $numerator *= ($n - $i + 1);
        $denominator *= $i;
    }
    
    return $numerator / $denominator;
}

$n = 5;
$k = 2;
echo binomialCoefficient($n, $k); // Output: 10