Combinations / Permutations

Hello Scrutinizer / Group ,

The shell script of awk that Scrutinizer made calculate all possible permutations in this case 3125 (5 numbers) but i want to have only the 126 possible combination. For now it does not matter the specific order of the combination numbers.

I would appreciate it you can help me with the awk script of combination.

Regards,
Carlos S

infile

1
2
3
4
5

Output, example

1+2+3+4+5
1+1+3+4+5
2+2+3+4+5
1+2+3+4+4
....
....
....

----------------------------------------------------------------

awk 'function perm(p,s,     i) {
       for(i=1;i<=n;i++)
         if(p==1)
           printf "%s%s=\n\n",s,A
         else
           perm(p-1,s A" + ")
     }
     {
       A[++n]=$1
     }
     END{
       perm(n)
     }' infile

Permutations:

awk '
function perm(l,v,i) {
    for(i in A) {
      if(l < length(A))
         perm(l+1, (v?v"+":x)i);
      else print v"+"i;
    }
}
{ A[$0] }
END {
   perm(1);
} ' infile

Combinations. Note: there are only 120 for 5 items (ie 5! = 120):

awk '
function comb(v,i) {
    for(i in A) {
      delete A;
      if(length(A))
         comb((v?v"+":x)i)
      else print v"+"i
      A;
    }
}
{ A[$0] }
END {
   comb();
} ' infile
1 Like