Coefficient binomial

    0

    0

    Giovanny Gongora

    Codiga's TypeScript Recipes

    Calculates the number of ways to choose k items from n items without repetition and without order.

    const binomialCoefficient = (n: number, k: number) => {
      if (k < 0 || k > n) return 0;
      if (k === 0 || k === n) return 1;
      if (k === 1 || k === n - 1) return n;
      if (n - k < k) k = n - k;
      let res: number = n;
      for (let j: number = 2; j <= k; j++) res *= (n - j + 1) / j;
      return Math.round(res);
    };
    
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.