js-combinatorics
Advanced tools
Comparing version 1.3.0 to 1.4.0
@@ -14,3 +14,3 @@ /** | ||
*/ | ||
export declare const version = "1.3.0"; | ||
export declare const version = "1.4.0"; | ||
/** | ||
@@ -53,2 +53,8 @@ * BigInt Workaround | ||
/** | ||
* returns the combinadics representation of `m` for `n C k`. | ||
* | ||
* @link https://en.wikipedia.org/wiki/Combinatorial_number_system | ||
*/ | ||
export declare function combinadic(n: any, k: any, m: any): any[]; | ||
/** | ||
* returns random integer `n` where `min` <= `n` < `max`: | ||
@@ -111,3 +117,3 @@ * | ||
export declare class Permutation extends _CBase { | ||
constructor(seed: any, size?: number); | ||
constructor(seed: Iterable<any>, size?: number); | ||
nth(n: anyint, nocheck?: boolean): Optional<any[]>; | ||
@@ -114,0 +120,0 @@ } |
@@ -14,3 +14,3 @@ /** | ||
*/ | ||
export const version = '1.3.0'; | ||
export const version = '1.4.0'; | ||
const _BI = typeof BigInt == 'function' ? BigInt : Number; | ||
@@ -76,4 +76,24 @@ /** | ||
/** | ||
* returns the combinadics representation of `m` for `n C k`. | ||
* | ||
* @link https://en.wikipedia.org/wiki/Combinatorial_number_system | ||
*/ | ||
export function combinadic(n, k, m) { | ||
let digits = []; | ||
let a = n; | ||
let b = k; | ||
let x = _BI(combination(n, k)) - _BI(1) - _BI(m); | ||
for (let i = 0; i < k; i++) { | ||
a--; | ||
while (x < combination(a, b)) | ||
a--; | ||
digits.push(n - 1 - a); | ||
x -= _BI(combination(a, b)); | ||
b--; | ||
} | ||
return digits; | ||
} | ||
/** | ||
* | ||
*/ | ||
const _crypto = typeof crypto !== 'undefined' ? crypto : {}; | ||
@@ -194,3 +214,3 @@ const _randomBytes = typeof _crypto['randomBytes'] === 'function' | ||
this.size = 0 < size && size <= this.seed.length ? size : this.seed.length; | ||
this.length = permutation(seed.length, this.size); | ||
this.length = permutation(this.seed.length, this.size); | ||
Object.freeze(this); | ||
@@ -220,6 +240,5 @@ } | ||
super(); | ||
const sseed = [...seed]; | ||
this.perm = new Permutation(sseed, size); | ||
this.size = this.perm.size; | ||
this.length = combination(sseed.length, this.size); | ||
this.seed = [...seed]; | ||
this.size = 0 < size && size <= this.seed.length ? size : this.seed.length; | ||
this.length = combination(this.seed.length, this.size); | ||
Object.freeze(this); | ||
@@ -231,14 +250,4 @@ } | ||
return undefined; | ||
function findIndex(n) { | ||
const [one, two] = typeof n === 'bigint' ? [_BI(1), _BI(2)] : [1, 2]; | ||
if (n <= two) | ||
return n; | ||
let p = n - one; | ||
let s = p & -p; | ||
let r = p + s; | ||
let t = r & -r; | ||
let m = ((t / s) >> one) - one; | ||
return r | m; | ||
} | ||
return this.perm.nth(findIndex(n), true); | ||
return combinadic(this.seed.length, this.size, n) | ||
.reduce((a, v) => a.concat(this.seed[v]), []); | ||
} | ||
@@ -245,0 +254,0 @@ } |
{ | ||
"name": "js-combinatorics", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"description": "Simple combinatorics like power set, combination, and permutation in JavaScript", | ||
@@ -5,0 +5,0 @@ "main": "combinatorics.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
31605
480