{"id":13555884,"url":"https://github.com/dankogai/js-combinatorics","last_synced_at":"2025-04-13T22:28:06.564Z","repository":{"id":7312827,"uuid":"8630837","full_name":"dankogai/js-combinatorics","owner":"dankogai","description":"power set, combination, permutation and more in JavaScript","archived":false,"fork":false,"pushed_at":"2024-02-21T14:50:05.000Z","size":334,"stargazers_count":742,"open_issues_count":4,"forks_count":98,"subscribers_count":36,"default_branch":"main","last_synced_at":"2024-05-21T22:04:19.748Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dankogai.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2013-03-07T15:54:02.000Z","updated_at":"2024-06-16T07:54:27.555Z","dependencies_parsed_at":"2022-08-19T12:40:36.840Z","dependency_job_id":"9fe0002e-d95c-42b7-b57d-45dfc8d69e5a","html_url":"https://github.com/dankogai/js-combinatorics","commit_stats":{"total_commits":216,"total_committers":23,"mean_commits":9.391304347826088,"dds":"0.19444444444444442","last_synced_commit":"1f7ae83d5d953f232e63936579730f2b13b06c0a"},"previous_names":[],"tags_count":37,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dankogai%2Fjs-combinatorics","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dankogai%2Fjs-combinatorics/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dankogai%2Fjs-combinatorics/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dankogai%2Fjs-combinatorics/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dankogai","download_url":"https://codeload.github.com/dankogai/js-combinatorics/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248790363,"owners_count":21161998,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T12:03:28.969Z","updated_at":"2025-04-13T22:28:06.513Z","avatar_url":"https://github.com/dankogai.png","language":"JavaScript","readme":"[![ES2020](https://img.shields.io/badge/JavaScript-ES2020-blue.svg)](https://tc39.es/ecma262/2020/)\n[![MIT LiCENSE](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)\n[![CI via GitHub Actions](https://github.com/dankogai/js-combinatorics/actions/workflows/node.js.yml/badge.svg)](https://github.com/dankogai/js-combinatorics/actions/workflows/node.js.yml)\n\njs-combinatorics\n================\n\nSimple combinatorics in JavaScript\n\n## HEADS UP: Version 2 and BigInt\n\nNow that [Internet Explorer has officially retired], It is safe to assume `BigInt` is available in every JavaScript environment.  From version 2.0 this module goes fully BigInt.  While integer arguments can still be either `number` or `bigint`, all integer values that can be `bigint` are always `bigint`, whereas previous versions may return `number` when the value \u003c= `Number.MAX_SAFE_INTEGER`.  It is not only more combinatorically natural, but also makes debugging easier especially on TypeScript.\n\n[Internet Explorer has officially retired]: https://blogs.windows.com/windowsexperience/2022/06/15/internet-explorer-11-has-retired-and-is-officially-out-of-support-what-you-need-to-know/\n[in every JavaScript environment]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt\n\n### For Swift programmers\n\nCheck [swift-combinatorics].  More naturally implemented with generics and protocol.\n\n[swift-combinatorics]: https://github.com/dankogai/swift-combinatorics\n\n## SYNOPSIS\n\n```javascript\nimport * as $C from './combinatorics.js';\nlet it =  new $C.Combination('abcdefgh', 4);\nfor (const elem of it) {\n  console.log(elem) // ['a', 'b', 'c', 'd'] ... ['e', 'f', 'g', 'h']\n}\n```\n\n## Usage\n\nload everything…\n\n```javascript\nimport * as Combinatorics from './combinatorics.js';\n```\n\nor just objects you want.\n\n```javascript\nimport { Combination, Permutation }  from './combinatorics.js';\n```\n\nYou don't even have to install if you `import` from CDNs.\n\n```javascript\nimport * as $C from 'https://cdn.jsdelivr.net/npm/js-combinatorics@2.1.2/combinatorics.min.js';\n```\n\nSince this is an ES6 module, `type=\"module\"` is required the `\u003cscript\u003e` tags. of your HTML files. But you can make it globally available as follows.\n\n```html\n\u003cscript type=\"module\"\u003e\n  import * as $C from 'combinatorics.js';\n  window.Combinatorics = $C;\n\u003c/script\u003e\n\u003cscript\u003e\n  // now you can access Combinatorics\n  let c = new Combinatorics.Combination('abcdefgh', 4);\n\u003c/script\u003e\n```\n\n### node.js REPL\n\n```shell\n% node\nWelcome to Node.js v16.15.0.\nType \".help\" for more information.\n\u003e const $C = await import('js-combinatorics')\nundefined\n\u003e $C\n[Module: null prototype] {\n  BaseN: [class BaseN extends _CBase],\n  CartesianProduct: [class CartesianProduct extends _CBase],\n  Combination: [class Combination extends _CBase],\n  Permutation: [class Permutation extends _CBase],\n  PowerSet: [class PowerSet extends _CBase],\n  combinadic: [Function: combinadic],\n  combination: [Function: combination],\n  factoradic: [Function: factoradic],\n  factorial: [Function: factorial],\n  permutation: [Function: permutation],\n  randomInteger: [Function: randomInteger],\n  version: '2.1.2'\n}\n\u003e [...new $C.Permutation('abcd')]\n[\n  [ 'a', 'b', 'c', 'd' ], [ 'a', 'b', 'd', 'c' ],\n  [ 'a', 'c', 'b', 'd' ], [ 'a', 'c', 'd', 'b' ],\n  [ 'a', 'd', 'b', 'c' ], [ 'a', 'd', 'c', 'b' ],\n  [ 'b', 'a', 'c', 'd' ], [ 'b', 'a', 'd', 'c' ],\n  [ 'b', 'c', 'a', 'd' ], [ 'b', 'c', 'd', 'a' ],\n  [ 'b', 'd', 'a', 'c' ], [ 'b', 'd', 'c', 'a' ],\n  [ 'c', 'a', 'b', 'd' ], [ 'c', 'a', 'd', 'b' ],\n  [ 'c', 'b', 'a', 'd' ], [ 'c', 'b', 'd', 'a' ],\n  [ 'c', 'd', 'a', 'b' ], [ 'c', 'd', 'b', 'a' ],\n  [ 'd', 'a', 'b', 'c' ], [ 'd', 'a', 'c', 'b' ],\n  [ 'd', 'b', 'a', 'c' ], [ 'd', 'b', 'c', 'a' ],\n  [ 'd', 'c', 'a', 'b' ], [ 'd', 'c', 'b', 'a' ]\n]\n\u003e \n```\n\n### commonjs (node.js)\n\n`./combinatorics.js` is an ECMAScript module but if you still need a UMD or commonjs version, they are available as `./umd/combinatorics.js` and `./commonjs/combinatorics.js` respectively.\n\n## Description\n\n### Arithmetic Functions\n\nSelf-explanatory, are they not?\n\n```javascript\nimport { permutation, combination, factorial, randomInteger } from './combinatorics.js';\n\npermutation(24, 12);  // 1295295050649600n\npermutation(26, 13);  // 64764752532480000n\n\ncombination(56, 28);  // 7648690600760440n\ncombination(58, 29);  // 30067266499541040n\n\nfactorial(18);  // 6402373705728000n\nfactorial(19);  // 121645100408832000n\n\nrandomInteger(6402373705727999);    // random n  [0,6402373705728000)\nrandomInteger(121645100408832000n); // ramdom n  [0n, 121645100408832000n)\n```\n\nThe arithmetic functions above accept both `Number` and `BigInt` (if supported).  Return answers always in `BigInt`.\n\n#### `factoradic()` and `combinadic()`\n\nThey need a little more explanation.\n\n```javascript\nimport { factoradic, combinadic } from './combinatorics.js';\n\nfactoradic(6402373705727999);     // [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17]\nfactoradic(121645100408831999n);  // [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18]\n\nconst c16_8 = combinadic(16, 8);\nc16_8(0);     // [ 0,  1,  2,  3,  4,  5,  6,  7]\nc16_8(12870); // [ 8,  9, 10, 11, 12, 13, 14, 15]\nconst c58_29 = combinadic(58, 29);\nc58_29(0); /* [\n   0,  1,  2,  3,  4,  5,  6,  7,  8,\n   9, 10, 11, 12, 13, 14, 15, 16, 17,\n  18, 19, 20, 21, 22, 23, 24, 25, 26,\n  27, 28\n] */\nc58_29(30067266499541039n); /* [\n  29, 30, 31, 32, 33, 34, 35, 36, 37,\n  38, 39, 40, 41, 42, 43, 44, 45, 46,\n  47, 48, 49, 50, 51, 52, 53, 54, 55,\n  56, 57\n] */\n```\n\n`factoradic(n)` returns the [factoradic] representation of `n`. For an array `ary` with `n` elements, you can get its `n`th permutation by picking `ary[i]` for each `i` in the factoradic.\n\n[factoradic]: https://en.wikipedia.org/wiki/Factorial_number_system\n\nUnlike other arithmetic functions, `combinadic()` returns a function which returns `m`th [combinadic] digit of `n C k`.  For an array `ary` with `n` elements, you can get its `m`th combination by picking `ary[i]` for each `i` in the combinadic.\n\n[combinadic]: https://en.wikipedia.org/wiki/Combinatorial_number_system\n\n### classes\n\nThe module comes with `Permutation`, `Combination`, `PowerSet`, `BaseN`, and `CartesianProduct`.  You can individually `import` them or all of them via `import *`\n\n```javascript\nimport * as $C from 'combinatorics.js';\n```\n\nYou construct an iterable object by giving a seed iterable and options.  in the example below, `'abcdefgh'` is the seed and `4` is the size of the element.\n\n```javascript\nlet it = new $C.Combination('abcdefgh', 4);\n```\n\nif you hate `new`, you can use `Klass.of` where `Klass` is one of the classes this module offers.\n\n```javascript\nlet it = $C.Combination.of('abcdefgh', 4);\n```\n\nOnce constructed, you can iterate via `for … of` statement or turn it into an array via `[...]` construct.\n\n```javascript\n[...it]; /* [\n  [ 'a', 'b', 'c', 'd' ], [ 'a', 'b', 'c', 'e' ], [ 'a', 'b', 'c', 'f' ],\n  [ 'a', 'b', 'c', 'g' ], [ 'a', 'b', 'c', 'h' ], [ 'a', 'b', 'd', 'e' ],\n  [ 'a', 'b', 'd', 'f' ], [ 'a', 'b', 'd', 'g' ], [ 'a', 'b', 'd', 'h' ],\n  [ 'a', 'b', 'e', 'f' ], [ 'a', 'b', 'e', 'g' ], [ 'a', 'b', 'e', 'h' ],\n  [ 'a', 'b', 'f', 'g' ], [ 'a', 'b', 'f', 'h' ], [ 'a', 'b', 'g', 'h' ],\n  [ 'a', 'c', 'd', 'e' ], [ 'a', 'c', 'd', 'f' ], [ 'a', 'c', 'd', 'g' ],\n  [ 'a', 'c', 'd', 'h' ], [ 'a', 'c', 'e', 'f' ], [ 'a', 'c', 'e', 'g' ],\n  [ 'a', 'c', 'e', 'h' ], [ 'a', 'c', 'f', 'g' ], [ 'a', 'c', 'f', 'h' ],\n  [ 'a', 'c', 'g', 'h' ], [ 'a', 'd', 'e', 'f' ], [ 'a', 'd', 'e', 'g' ],\n  [ 'a', 'd', 'e', 'h' ], [ 'a', 'd', 'f', 'g' ], [ 'a', 'd', 'f', 'h' ],\n  [ 'a', 'd', 'g', 'h' ], [ 'a', 'e', 'f', 'g' ], [ 'a', 'e', 'f', 'h' ],\n  [ 'a', 'e', 'g', 'h' ], [ 'a', 'f', 'g', 'h' ], [ 'b', 'c', 'd', 'e' ],\n  [ 'b', 'c', 'd', 'f' ], [ 'b', 'c', 'd', 'g' ], [ 'b', 'c', 'd', 'h' ],\n  [ 'b', 'c', 'e', 'f' ], [ 'b', 'c', 'e', 'g' ], [ 'b', 'c', 'e', 'h' ],\n  [ 'b', 'c', 'f', 'g' ], [ 'b', 'c', 'f', 'h' ], [ 'b', 'c', 'g', 'h' ],\n  [ 'b', 'd', 'e', 'f' ], [ 'b', 'd', 'e', 'g' ], [ 'b', 'd', 'e', 'h' ],\n  [ 'b', 'd', 'f', 'g' ], [ 'b', 'd', 'f', 'h' ], [ 'b', 'd', 'g', 'h' ],\n  [ 'b', 'e', 'f', 'g' ], [ 'b', 'e', 'f', 'h' ], [ 'b', 'e', 'g', 'h' ],\n  [ 'b', 'f', 'g', 'h' ], [ 'c', 'd', 'e', 'f' ], [ 'c', 'd', 'e', 'g' ],\n  [ 'c', 'd', 'e', 'h' ], [ 'c', 'd', 'f', 'g' ], [ 'c', 'd', 'f', 'h' ],\n  [ 'c', 'd', 'g', 'h' ], [ 'c', 'e', 'f', 'g' ], [ 'c', 'e', 'f', 'h' ],\n  [ 'c', 'e', 'g', 'h' ], [ 'c', 'f', 'g', 'h' ], [ 'd', 'e', 'f', 'g' ],\n  [ 'd', 'e', 'f', 'h' ], [ 'd', 'e', 'g', 'h' ], [ 'd', 'f', 'g', 'h' ],\n  [ 'e', 'f', 'g', 'h' ]\n] */\n```\n\n#### `.length`\n\nThe object has `.length` so you don't have to iterate to count the elements.  Note the value is in `bigint` so you may need to convert to `number`.\n\n```javascript\nit.length       // 70n\n[...it].length  // 70\nit.length ==  [...it].length // true because comparisons work between number and bigint\nit.length === [...it].length // false because types are different\n```\n\n#### `.at()` (or `.nth()`)\n\nAnd the object has `.at(n)` method so you can random-access each element.  This is the equivalent of subscript in `Array`.  It was previously named `.nth()` but it was renamed to `.at()` ala `Array.prototype.at()` in ES2020. `.nth()` still available for backward compatibility.\n\n```javascript\nit.at(0);  //  [ 'a', 'b', 'c', 'd' ];\nit.at(69); //  [ 'a', 'd', 'c', 'h' ];\n```\n\n`at()` accepts both `Number` and `BigInt`.\n\n```javascript\nit.at(69n);  // [ 'a', 'd', 'c', 'h' ];\n```\n\n`at()` also accepts negative indexes.   In which case `n` is `(-n)th` element from `.length`.\n\n```javascript\nit.at(-1);   // [ 'a', 'd', 'c', 'h' ]\nit.at(-70);  // [ 'a', 'b', 'c', 'd' ]\n```\n\n#### `.sample()`\n\nAnd `.sample()` picks random element, which is defined as `.at(randomInteger(.length))`.\n\n```javascript\nit.sample() // one of ['a', 'b', 'c', 'd'] ... ['a', 'd', 'e', 'f']\n```\n\n### Beyond `Number.MAX_SAFE_INTEGER`\n\nOccasionally you need `BigInt` to access elements beyond `Number.MAX_SAFE_INTEGER`.\n\n```javascript\nit = new $C.Permutation('abcdefghijklmnopqrstuvwxyz');\nit.length;  // 403291461126605635584000000n\n```\n\nYou can still access elements before `Number.MAX_SAFE_INTEGER` in `Number`.\n\n```javascript\nit.at(0);  /* [\n  'a', 'b', 'c', 'd', 'e', 'f',\n  'g', 'h', 'i', 'j', 'k', 'l',\n  'm', 'n', 'o', 'p', 'q', 'r',\n  's', 't', 'u', 'v', 'w', 'x',\n  'y', 'z'\n] */\nit.at(9007199254740990); /* [\n  'a', 'b', 'c', 'd', 'e', 'f',\n  'g', 'i', 'p', 'n', 'r', 'z',\n  'm', 'h', 'y', 'x', 'u', 't',\n  'l', 'j', 'k', 'q', 's', 'o',\n  'v', 'w'\n] */\n```\n\nBut how are you goint to acccess elements beyond that?  Just use `BigInt`.\n\n```javascript\nit.at(9007199254740991n);  /* [\n  'a', 'b', 'c', 'd', 'e', 'f',\n  'g', 'i', 'p', 'n', 'r', 'z',\n  'm', 'h', 'y', 'x', 'u', 't',\n  'l', 'j', 'k', 'q', 's', 'o',\n  'w', 'v'\n] */\nit.at(it.length - 1n); /* [\n  'z', 'y', 'x', 'w', 'v', 'u',\n  't', 's', 'r', 'q', 'p', 'o',\n  'n', 'm', 'l', 'k', 'j', 'i',\n  'h', 'g', 'f', 'e', 'd', 'c',\n  'b', 'a'\n] */\n```\n\nYou can tell if you need `BigInt` via `.isBig`.  Note `.length` is always `bigint` from version 2.0 so you may not need this method any more.  So it is now deprecated.\n\n```javascript\nnew $C.Permutation('0123456789').isBig; // false\nnew $C.Permutation('abcdefghijklmnopqrstuvwxyz').isBig; // true\n```\n\nYou can also check if it is safe on your platform via `.isSafe`.  It is now deprecated for the same reason as `.isBig`.\n\n```javascript\nnew $C.Permutation('abcdefghijklmnopqrstuvwxyz').isSafe; // always true\n```\n\n### class `Permutation`\n\nAn iterable which permutes a given iterable.\n\n`new Permutation(seed, size)`\n\n* `seed`: the seed iterable.   `[...seed]` becomes the seed array.\n* `size`: the number of elements in the iterated element.  defaults to `seed.length`\n\n````javascript\nimport {Permutation} from './combinatorics.js';\n\nlet it = new Permutation('abcd'); // size 4 is assumed\nit.length;  // 24n\n[...it];    /* [\n  [ 'a', 'b', 'c', 'd' ], [ 'a', 'b', 'd', 'c' ],\n  [ 'a', 'c', 'b', 'd' ], [ 'a', 'c', 'd', 'b' ],\n  [ 'a', 'd', 'b', 'c' ], [ 'a', 'd', 'c', 'b' ],\n  [ 'b', 'a', 'c', 'd' ], [ 'b', 'a', 'd', 'c' ],\n  [ 'b', 'c', 'a', 'd' ], [ 'b', 'c', 'd', 'a' ],\n  [ 'b', 'd', 'a', 'c' ], [ 'b', 'd', 'c', 'a' ],\n  [ 'c', 'a', 'b', 'd' ], [ 'c', 'a', 'd', 'b' ],\n  [ 'c', 'b', 'a', 'd' ], [ 'c', 'b', 'd', 'a' ],\n  [ 'c', 'd', 'a', 'b' ], [ 'c', 'd', 'b', 'a' ],\n  [ 'd', 'a', 'b', 'c' ], [ 'd', 'a', 'c', 'b' ],\n  [ 'd', 'b', 'a', 'c' ], [ 'd', 'b', 'c', 'a' ],\n  [ 'd', 'c', 'a', 'b' ], [ 'd', 'c', 'b', 'a' ]\n] */\n\nit = new Permutation('abcdefghijklmnopqrstuvwxyz0123456789');\nit.length;  // 371993326789901217467999448150835200000000n\nit.at(371993326789901217467999448150835199999999n);  /* [\n  '9', '8', '7', '6', '5', '4', '3',\n  '2', '1', '0', 'z', 'y', 'x', 'w',\n  'v', 'u', 't', 's', 'r', 'q', 'p',\n  'o', 'n', 'm', 'l', 'k', 'j', 'i',\n  'h', 'g', 'f', 'e', 'd', 'c', 'b',\n  'a'\n] */\n````\n\nMaking a permutation of the iterable then taking its sample is functionally the same as [Fisher–Yates shuffle] of the iterable.  Instead of shuffling the deck, it make all possible cases available and let you pick one.\n\n```javascript\nit.sample(); // something between ['a','b', ... '9'] and ['9','8',....'a'] \n```\n\nIt is in fact a little better because `.sample()` only needs one random number (between 0 and `.length - 1`) while Fisher–Yates needs `n` random numbers.\n\n[Fisher–Yates shuffle]: https://en.wikipedia.org/wiki/Fisher–Yates_shuffle\n\n### class `Combination`\n\nAn iterable which emits a combination of a given iterable.\n\n`new Combination(seed, size)`\n\n* `seed`: the seed iterable.\n* `size`: the number of elements in the iterated element.  \n\n````javascript\nimport {Combination} from './combinatorics.js';\n\nlet it = new Combination('abcd', 2);\nit.length;  // 6n\n[...it];    /* [\n  [ 'a', 'b' ],\n  [ 'a', 'c' ],\n  [ 'a', 'd' ],\n  [ 'b', 'c' ],\n  [ 'b', 'd' ],\n  [ 'c', 'd' ]\n] */\n\nlet a100 = Array(100).fill(0).map((v,i)=\u003ei); // [0, 1, ...99]\nit = new Combination(a100, 50);\nit.length;  // 100891344545564193334812497256n\nit.at(100891344545564193334812497255n);  /* [\n  50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,\n  61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71,\n  72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82,\n  83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93,\n  94, 95, 96, 97, 98, 99\n] */\n````\n\n### class `PowerSet`\n\nAn iterable which emits each element of its power set.\n\n`new PowerSet(seed)`\n\n* `seed`: the seed iterable.\n\n````javascript\nimport {PowerSet} from './combinatorics.js';\n\nlet it = new PowerSet('abc');\nit.length;  // 8n\n[...it];    /* [\n  [],\n  [ 'a' ],\n  [ 'b' ],\n  [ 'a', 'b' ],\n  [ 'c' ],\n  [ 'a', 'c' ],\n  [ 'b', 'c' ],\n  [ 'a', 'b', 'c' ]\n] */\n\nit = new PowerSet(\n  'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'\n);\nit.length;  // 18446744073709551616n\nit.at(18446744073709551615n);  /* [\n  'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',\n  'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',\n  'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a',\n  'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',\n  'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',\n  't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1',\n  '2', '3', '4', '5', '6', '7', '8', '9', '+',\n  '/'\n] */\n````\n\n### class `BaseN`\n\nAn iterable which emits all numbers in the given system.\n\n`new BaseN(seed, size)`\n\n* `seed`: the seed iterable whose elements represent digits.\n* `size`: the number of digits\n\n```javascript\nimport {BaseN} from './combinatorics.js';\n\nlet it = new BaseN('abc', 3);\nit.length;  // 27n\n[...it];    /* [\n  [ 'a', 'a', 'a' ], [ 'b', 'a', 'a' ],\n  [ 'c', 'a', 'a' ], [ 'a', 'b', 'a' ],\n  [ 'b', 'b', 'a' ], [ 'c', 'b', 'a' ],\n  [ 'a', 'c', 'a' ], [ 'b', 'c', 'a' ],\n  [ 'c', 'c', 'a' ], [ 'a', 'a', 'b' ],\n  [ 'b', 'a', 'b' ], [ 'c', 'a', 'b' ],\n  [ 'a', 'b', 'b' ], [ 'b', 'b', 'b' ],\n  [ 'c', 'b', 'b' ], [ 'a', 'c', 'b' ],\n  [ 'b', 'c', 'b' ], [ 'c', 'c', 'b' ],\n  [ 'a', 'a', 'c' ], [ 'b', 'a', 'c' ],\n  [ 'c', 'a', 'c' ], [ 'a', 'b', 'c' ],\n  [ 'b', 'b', 'c' ], [ 'c', 'b', 'c' ],\n  [ 'a', 'c', 'c' ], [ 'b', 'c', 'c' ],\n  [ 'c', 'c', 'c' ]\n] */\n\nit = BaseN('0123456789abcdef', 16);\nit.length;  // 18446744073709551616n\nit.at(18446744073709551615n);  /* [\n  'f', 'f', 'f', 'f',\n  'f', 'f', 'f', 'f',\n  'f', 'f', 'f', 'f',\n  'f', 'f', 'f', 'f'\n] */\n```\n\n### class `CartesianProduct`\n\nA [cartesian product] of given sets.\n\n[cartesian Product]: https://en.wikipedia.org/wiki/Cartesian_product\n\n`new CartesianProduct(...args)`\n\n* `args`: iterables that represent sets\n\n```javascript\nimport {CartesianProduct} from './combinatorics.js';\n\nlet it = new CartesianProduct('012','abc','xyz');\nit.length;  // 27n\n[...it];    /* [\n  [ '0', 'a', 'x' ], [ '1', 'a', 'x' ],\n  [ '2', 'a', 'x' ], [ '0', 'b', 'x' ],\n  [ '1', 'b', 'x' ], [ '2', 'b', 'x' ],\n  [ '0', 'c', 'x' ], [ '1', 'c', 'x' ],\n  [ '2', 'c', 'x' ], [ '0', 'a', 'y' ],\n  [ '1', 'a', 'y' ], [ '2', 'a', 'y' ],\n  [ '0', 'b', 'y' ], [ '1', 'b', 'y' ],\n  [ '2', 'b', 'y' ], [ '0', 'c', 'y' ],\n  [ '1', 'c', 'y' ], [ '2', 'c', 'y' ],\n  [ '0', 'a', 'z' ], [ '1', 'a', 'z' ],\n  [ '2', 'a', 'z' ], [ '0', 'b', 'z' ],\n  [ '1', 'b', 'z' ], [ '2', 'b', 'z' ],\n  [ '0', 'c', 'z' ], [ '1', 'c', 'z' ],\n  [ '2', 'c', 'z' ]\n] */\n```\n\nSince the number of arguments to `CartesianProduct` is variable, it is sometimes helpful to give a single array with all arguments.   But you cannot `new ctor.apply(null, args)` this case.  To mitigate that, you can use `.from()`.\n\n```javascript\nlet a16 =  Array(16).fill('0123456789abcdef');\nit = CartesianProduct.from(a16);\nit.length;  // 18446744073709551616n\nit.at(18446744073709551615n);  /* [\n  'f', 'f', 'f', 'f',\n  'f', 'f', 'f', 'f',\n  'f', 'f', 'f', 'f',\n  'f', 'f', 'f', 'f'\n] */\n````\n\n## What's new from version 0.x?\n\n`js-combinatorics` has gone ES2015 since version 1.\n\n* native iterator instead of custom\n* module. `import` instead of `require`.\n* `BigInt` where possible\n\nAnd from version 1.2 it is written in TypeScript.  `combinatorics.js` and  `combinatorics.d.ts` are compiled from `combinatorics.ts`.\n\nAPIs will change accordingly.  Old versions are available in the `version0` branch.\n\n### What's gone from version 0.x?\n\n* `bigCombination` is gone because all classes now can handle big -- combinatorially big! -- cases thanks to [BigInt] support getting standard.  Safari 13 and below is a major exception but BigInt is coming to Safari 14 and up.\n\n[BigInt]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt\n\n* `permutationCombination` is gone because the name is misleading and it is now trivially easy to reconstruct as follow:\n\n```javascript\nclass permutationCombination {\n    constructor(seed) {\n        this.seed = [...seed];\n    }\n    [Symbol.iterator]() {\n        return function*(it){\n            for (let i = 1, l = it.length; i \u003c= l; i++) {\n                yield* new Permutation(it, i);\n            }\n        }(this.seed);\n    }\n}\n```\n\n* `js-combinatorics` is now natively iterable.  Meaning its custom iterators are gone -- with its methods like `.map` and `.filter`.  JS iterators are very minimalistic with only `[...]` and `for ... of`.  But don't worry.  There are several ways to make those functional methods back again. \n\nFor instance, You can use [js-xiterable] like so:\n\n[js-xiterable]: https://github.com/dankogai/js-xiterable\n\n```javascript\nimport {xiterable as $X} from \n  'https://cdn.jsdelivr.net/npm/js-xiterable@0.0.3/xiterable.min.js';\nimport {Permutation} from 'combinatorics.js';\nlet it = new Permutation('abcd');\nlet words = $X(it).map(v=\u003ev.join(''))\nfor (const word of words)) console.log(word)\n/*\nabcd\nabdc\n...\ndcab\ndcba\n*/\n```\n","funding_links":[],"categories":["JavaScript","others"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdankogai%2Fjs-combinatorics","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdankogai%2Fjs-combinatorics","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdankogai%2Fjs-combinatorics/lists"}