dankogai / js-combinatorics

power set, combination, permutation and more in JavaScript
MIT License
742 stars 97 forks source link

`permutation` with larger arrays (> 32 entries) issue #34

Closed dandar3 closed 7 years ago

dandar3 commented 8 years ago

permutation() behaves strangely with input arrays with more than 32 entries.

Somehow it seems to only consider the first number of entries as difference from a multiple of 32 - e.g. from an input of 71 entries it only used the first 7 [ 71 - 2 * 32 = 7 ].

Poking around permutation() function and noticing the bigCombination() variant, I found that using that instead of combination() makes it work correctly.

I don't know whether you want to introduce a new bigPermutation() function or maybe just patch existing permutation() function:

var permutation = function(ary, nelem, fun) {
        [...]
        addProperties(that, {
            valueOf: function() {
                return size;
            },
            init: function() {
                this.cmb = (ary.length <= 32 ? combination(ary, nelem) : bigCombination(ary, nelem));
                this.per = _permutation(this.cmb.next());
            },
        [...]
}