-1

I've looked everywhere, but can't find a workable solution to this question that would suit my purposes. Quite simply, I'm looking to generate all the permutations of n - the number of items in a pool, selecting k - the number of objects to choose each time. With no repetitions. For example, I have a basket of 8 billiard balls numbered 1 to 8, and I choose 3, put them back and choose 3 more and continue until all possible permutations have been selected. The sheet function =permut(8, 3) = 336. But what are the permutations that can be produced? I'm looking for a list. I've come across various suggestions, but nothing like what this website does: Permutation generator of N items from a pool of K to without repetitions. A sample of the result is: Sample output

The website is fine, but you have to download the results and import them into a spreadsheet, which gets pretty onerous after a pool of 10 or 11 items, and even worse when having to work with such large outputs. Better to have a script run through the list and analyze each selection as it's generated and output only the hits. Not to mention that N and K can change depending on the user's needs.

I'm moderately proficient in Google script, so if something already exists out there, I could probably modify it to make work.

I thank you for your time.

Frank
  • 40
  • 1
  • 5
  • https://stackoverflow.com/questions/73752135/how-to-list-all-permutations-without-repetition It should be much easier to implement any of the same in apps script. – TheMaster Dec 11 '22 at 16:11

1 Answers1

0

Here is a script that produces the permutations of a set n=8, k=3.

function permutations() {
  try {
    let n = 8;
    let k = 3;
    let results =[];
    for( let i=0; i<n; i++ ) {
      for( let j=0; j<n; j++ ) {
        for( let l=0; l<n; l++ ) {
          if( j == i || l == i || l == j ) continue;
          results.push( [ i+1, j+1, l+1 ] );
        }
      }
    }
    function factorial(m) {
      if(m == 0 || m == 1){
        return 1;
      //recursive case
      }else{
        return m * factorial(m-1);
      }
    }
    console.log(factorial(n)/factorial(n-k));
    console.log(results.length);
    let sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Permutations");
    sheet.getRange(1,1,results.length,3).setValues(results);
  }
  catch(err) {
    throw "Error in test: "+err;
  }
}

Ececution log

8:38:02 AM  Notice  Execution started
8:38:04 AM  Info    336
8:38:04 AM  Info    336
8:38:04 AM  Notice  Execution completed

And in case you are interested here is a script to produce the combinations

function combinations() {
  try {
    let n = 8;
    let k = 3;
    let results =[];
    for( let i=0; i<n; i++ ) {
      for( let j=i+1; j<n; j++ ) {
        for( let l=j+1; l<n; l++ ) {
          results.push( [ i+1, j+1, l+1 ] );
        }
      }
    }
    console.log(results.length);
    let sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Permutations");
    sheet.getRange(1,1,results.length,3).setValues(results);
  }
  catch(err) {
    throw "Error in test: "+err;
  }
}

Execution log

8:39:11 AM  Notice  Execution started
8:39:12 AM  Info    56
8:39:12 AM  Notice  Execution completed
TheWizEd
  • 7,517
  • 2
  • 11
  • 19