{
  "schemaVersion": 1,
  "slug": "permutations-calculator",
  "category": "math",
  "kind": "implementation",
  "independentValidation": false,
  "implementationHash": "adb681163cb4b94a525a3c7ee37162556550bc326d5861c780484f115b4d60aa",
  "provenance": "Generated from the calculation contract used by the published page. Publication is not external validation.",
  "sourceFiles": [
    "data/calculators/permutations-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": "P(N,R) = N! ÷ (N − R)!; C(N,R) = N! ÷ (R! × (N − R)!); P_rep = N^R",
    "vars": [
      [
        "N",
        "Total items"
      ],
      [
        "R",
        "Items chosen"
      ],
      [
        "P",
        "Permutations"
      ],
      [
        "C",
        "Combinations"
      ],
      [
        "P_rep",
        "Permutations with repetition"
      ]
    ],
    "note": "Inputs must be whole numbers from 0 to 100. Without repetition, permutations and combinations are 0 when R > N; permutations with repetition and N! are still calculated. Counts are returned as exact integer text."
  },
  "externalEvidence": null,
  "contract": {
    "compute": "const N=Number(n),R=Number(r);\nconst fail=()=>({permutations:null,combinations:null,permutationsWithRepetition:null,factorialN: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;};\nlet factorial=1n;for(let i=2;i<=N;i++)factorial*=BigInt(i);\nreturn {permutations:perm(N,R).toString(),combinations:choose(N,R).toString(),permutationsWithRepetition:(BigInt(N)**BigInt(R)).toString(),factorialN:factorial.toString()};",
    "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": "permutations",
        "precision": 0,
        "unit": "",
        "type": "text"
      },
      {
        "name": "combinations",
        "precision": 0,
        "unit": "",
        "type": "text"
      },
      {
        "name": "permutationsWithRepetition",
        "precision": 0,
        "unit": "",
        "type": "text"
      },
      {
        "name": "factorialN",
        "precision": 0,
        "unit": "",
        "type": "text"
      }
    ],
    "interpret": null
  }
}
