{
  "schemaVersion": 1,
  "slug": "combinations-calculator",
  "category": "math",
  "kind": "implementation",
  "independentValidation": false,
  "implementationHash": "c5b1859602c6f1ea35ebee1f32f657acdd19a9920e722a34e82362b2712084ab",
  "provenance": "Generated from the calculation contract used by the published page. Publication is not external validation.",
  "sourceFiles": [
    "data/calculators/combinations-calculator.json",
    "src/engine.js"
  ],
  "runtimeSource": "/sources/engine.txt",
  "runtimeHash": "5c888d8898afbdee36267bb9977829e1e2ac568f6677c8ee87469c139864645c",
  "scope": "The compute function records implemented equations, constants, lookup tables and custom rules. Inputs record defaults and allowed values; outputs record precision and units. Defaults are examples, not recommendations. General background references are not evidence for this specific algorithm.",
  "publishedFormula": {
    "formula": "C(n,r) = n! ÷ (r!(n−r)!); P(n,r) = n! ÷ (n−r)!; C_rep = (n+r−1)! ÷ (r!(n−1)!)",
    "vars": [
      [
        "n",
        "total items"
      ],
      [
        "r",
        "items chosen"
      ],
      [
        "C(n,r)",
        "combinations"
      ],
      [
        "P(n,r)",
        "permutations"
      ],
      [
        "C_rep",
        "combinations with repetition"
      ]
    ],
    "note": "Inputs must be whole numbers from 0 to 100. Without repetition, results are 0 when r > n; combinations with repetition are calculated separately. Counts are returned as exact integer text."
  },
  "externalEvidence": null,
  "contract": {
    "compute": "const N=Number(n),R=Number(r);\nconst fail=()=>({combinations:null,permutations:null,withRepetition:null,__error:'inputError'});\nif(!Number.isInteger(N)||!Number.isInteger(R)||N<0||R<0||N>100||R>100)return fail();\nconst choose=(total,k)=>{if(k<0||k>total)return 0n;k=Math.min(k,total-k);let out=1n;for(let i=1;i<=k;i++)out=out*BigInt(total-k+i)/BigInt(i);return out;};\nconst perm=(total,k)=>{if(k<0||k>total)return 0n;let out=1n;for(let i=0;i<k;i++)out*=BigInt(total-i);return out;};\nconst combinations=choose(N,R).toString(),permutations=perm(N,R).toString();\nconst withRepetition=(N===0?(R===0?1n:0n):choose(N+R-1,R)).toString();\nreturn {combinations,permutations,withRepetition};",
    "inputs": [
      {
        "name": "n",
        "type": "number",
        "default": 10,
        "min": 0,
        "max": 100,
        "step": 1,
        "slider": true
      },
      {
        "name": "r",
        "type": "number",
        "default": 3,
        "min": 0,
        "max": 100,
        "step": 1,
        "slider": true
      }
    ],
    "outputs": [
      {
        "name": "combinations",
        "precision": 0,
        "unit": "",
        "type": "text"
      },
      {
        "name": "permutations",
        "precision": 0,
        "unit": "",
        "type": "text"
      },
      {
        "name": "withRepetition",
        "precision": 0,
        "unit": "",
        "type": "text"
      }
    ],
    "interpret": null
  }
}
