{"record":{"id":"c7d928d4126d74f3","repo":"gchq/CyberChef","slug":"too-many-permutations-please-reduce-k-n-to-under","errorCode":null,"errorMessage":"Too many permutations, please reduce k^n to under 50,000.","messagePattern":"Too many permutations, please reduce k\\^n to under 50,000\\.","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"warning","filePath":"src/core/operations/GenerateDeBruijnSequence.mjs","lineNumber":66,"sourceCode":"\n        if (k < 2 || k > 9) {\n            throw new OperationError(\"Invalid alphabet size, required to be between 2 and 9 (inclusive).\");\n        }\n\n        if (!Number.isInteger(k)) {\n            throw new OperationError(\"Invalid alphabet size, required to be integer.\");\n        }\n\n        if (!Number.isInteger(n)) {\n            throw new OperationError(\"Invalid key length, required to be integer.\");\n        }\n\n        if (n < 2) {\n            throw new OperationError(\"Invalid key length, required to be at least 2.\");\n        }\n\n        if (Math.pow(k, n) > 50000) {\n            throw new OperationError(\"Too many permutations, please reduce k^n to under 50,000.\");\n        }\n\n        const a = new Array(k * n).fill(0);\n        const sequence = [];\n\n        (function db(t = 1, p = 1) {\n            if (t > n) {\n                if (n % p !== 0) return;\n                for (let j = 1; j <= p; j++) {\n                    sequence.push(a[j]);\n                }\n                return;\n            }\n\n            a[t] = a[t - p];\n            db(t + 1, p);\n            for (let j = a[t - p] + 1; j < k; j++) {\n                a[t] = j;","sourceCodeStart":48,"sourceCodeEnd":84,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/GenerateDeBruijnSequence.mjs#L48-L84","documentation":"Final validation in Generate De Bruijn Sequence: rejects inputs where k^n exceeds 50,000. The De Bruijn sequence length equals k^n, and the generator materializes supporting arrays sized by k*n, so this cap prevents excessive memory/CPU. It is a resource guard, not a mathematical constraint.","triggerScenarios":"Any (k, n) pair whose exponentiation exceeds 50,000, e.g. k=9,n=5 (59049) or k=3,n=10 (59049) or k=2,n=16 (65536).","commonSituations":"Pushing for maximum coverage keycodes; not realizing sequence length grows exponentially; nudge n up expecting linear cost.","solutions":["Reduce n so that Math.pow(k, n) <= 50000 (e.g. for k=9 keep n<=4; for k=2 keep n<=15).","Alternatively reduce k.","Compute k*n and Math.pow(k,n) before running to confirm the budget."],"exampleFix":"// before\nargs = [9, 5]; // 9^5 = 59049 > 50000\n// after\nargs = [9, 4]; // 9^4 = 6561","handlingStrategy":"validation","validationCode":"if (Math.pow(k, n) > 50000) throw new Error(`k^n = ${Math.pow(k,n)} exceeds 50000; reduce k or n`);","typeGuard":"/** @param {number} k @param {number} n */\nfunction withinBudget(k,n){return Math.pow(k,n) <= 50000;}","tryCatchPattern":null,"preventionTips":["Compute Math.pow(k,n) before running; sequence length equals k^n.","Reduce n first (it grows the result fastest), then k.","For k=9 cap n at 4; for k=2 cap n at 15."],"tags":["combinatorics","de-bruijn","argument-validation","resource-limits"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}