dankogai / js-combinatorics

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

baseN size limiter #60

Closed carloshximenes closed 5 years ago

carloshximenes commented 5 years ago

I'm using baseN to create a 11x11 array.

const values = [1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2];
const allCombinations = Combinatorics.baseN(values, 11).toArray();

I'm with the out-of-memory error. So i'm wondering if is there a way to limit my result string to show my only the firsts X results?

Thanks.

blanchg commented 5 years ago

Instead of calling toArray use the iterator to get just the results you need. You can get them one at a time then with minimal memory usage.

On Wed., 9 Oct. 2019, 4:40 am Carlos Henrique Ximenes de Figueiredo Mota, < notifications@github.com> wrote:

I'm using baseN to create a 11x11 array.

const values = [1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2]; const allCombinations = Combinatorics.baseN(values, 11).toArray();

I'm with the out-of-memory error. So i'm wondering if is there a way to limit my result string to show my only the firsts X results?

Thanks.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/dankogai/js-combinatorics/issues/60?email_source=notifications&email_token=AAAVDX5IEHBWOJQCHAO5CZ3QNTHZHA5CNFSM4I6VUS4KYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4HQNUUNQ, or mute the thread https://github.com/notifications/unsubscribe-auth/AAAVDXYMWXQBMS4ULBLA77LQNTHZHANCNFSM4I6VUS4A .

carloshximenes commented 5 years ago

Sorry, I'm new on JS so can you help me with an example? Thanks.

blanchg commented 5 years ago

Look at the Generator Methods in the documentation https://github.com/dankogai/js-combinatorics#generator-methods

let cmb = Combinatorics.combination(['a','b','c','d'], 2); let i = 0; while(a = cmb.next() && i < 10) { console.log(a); i++; }

On Wed, 9 Oct 2019 at 08:44, Carlos Henrique Ximenes de Figueiredo Mota < notifications@github.com> wrote:

Sorry, I'm new on JS so can you help me with an example? Thanks.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/dankogai/js-combinatorics/issues/60?email_source=notifications&email_token=AAAVDX3NAI6YNRZG7SC2ANTQNUEOPA5CNFSM4I6VUS4KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEAV3HKY#issuecomment-539734955, or mute the thread https://github.com/notifications/unsubscribe-auth/AAAVDXZQNQETT5Y5I5GCOPDQNUEOPANCNFSM4I6VUS4A .

carloshximenes commented 5 years ago

Thanks!

const values = [1,2,3,4,5,6,7,8,9];
const allCombinations = jsCombinatorics.baseN(values, 11);

const results = [];
for (let i = 0; i < 5; i++) {
  results.push(allCombinations.next());
}

console.log(results);