Skip to main content
variable binomialCoefficient
import { binomialCoefficient } from "https://deno.land/x/30_seconds_of_typescript@v1.0.1/util.ts";

Evaluates the binomial coefficient of two integers n and k.

Use Number.isNaN() to check if any of the two values is NaN. Check if k is less than 0, greater than or equal to n, equal to 1 or n - 1 and return the appropriate result. Check if n - k is less than k and switch their values accordingly. Loop from 2 through k and calculate the binomial coefficient. Use Math.round() to account for rounding errors in the calculation.

type

(n: number, k: number) => number