Permutations Dynamic Array Formulas

lrobbo314

Well-known Member
Joined
Jul 14, 2008
Messages
3,927
Office Version
  1. 365
Platform
  1. Windows
Using the dynamic array functions, I created a formula that will return permutations, with or without repetitions, based on a range of values you select. I used the lambda function to turn it into an easy-to-use formula.

Just wanted to share because I think it's cool, permutations come up a lot, and I'd like to see what everyone thinks. I'm sure that this formula could be optimized or shortened.

permutationx.xlsm
ABCDEFGH
1ValuesRepitionsResults
2Dog0DogCatBirdLizard
3CatDogCatLizardBird
4BirdDogBirdCatLizard
5LizardDogBirdLizardCat
6DogLizardCatBird
7DogLizardBirdCat
8CatDogBirdLizard
9CatDogLizardBird
10CatBirdDogLizard
11CatBirdLizardDog
12CatLizardDogBird
13CatLizardBirdDog
14BirdDogCatLizard
15BirdDogLizardCat
16BirdCatDogLizard
17BirdCatLizardDog
18BirdLizardDogCat
19BirdLizardCatDog
20LizardDogCatBird
21LizardDogBirdCat
22LizardCatDogBird
23LizardCatBirdDog
24LizardBirdDogCat
25LizardBirdCatDog
Sheet3
Cell Formulas
RangeFormula
E2:H25E2=PermutX(A2:A5,C2)
Dynamic array formulas.


permutationx.xlsm
ABCDEFG
1ValuesRepitionsResults
2Dog1DogDogDog
3CatDogDogCat
4BirdDogDogBird
5DogCatDog
6DogCatCat
7DogCatBird
8DogBirdDog
9DogBirdCat
10DogBirdBird
11CatDogDog
12CatDogCat
13CatDogBird
14CatCatDog
15CatCatCat
16CatCatBird
17CatBirdDog
18CatBirdCat
19CatBirdBird
20BirdDogDog
21BirdDogCat
22BirdDogBird
23BirdCatDog
24BirdCatCat
25BirdCatBird
26BirdBirdDog
27BirdBirdCat
28BirdBirdBird
Sheet3
Cell Formulas
RangeFormula
E2:G28E2=PermutX(A2:A5,C2)
Dynamic array formulas.


Name Manager Lambda function "PermutX"
Excel Formula:
=LAMBDA(
    range,
    repetition,
        LET(v,range,
        c,COUNTA(v),
        n,MID(TEXT(BASE(SEQUENCE(PERMUTATIONA(c,c),,0),c),REPT("0",c)),SEQUENCE(,c),1)+1,
        pa,INDEX(v,n),nj,BYROW(pa,LAMBDA(ro,TEXTJOIN(,,ro))),
        f,BYROW(nj,LAMBDA(ro,SUM(IF(LEN(ro)-LEN(SUBSTITUTE(ro,v,"")),1,0))=c)),
        IF(repetition,pa,FILTER(pa,f))
        )
    )
 
Well, it's barely possible. I like challenges, so I took a shot at it. Fluff pretty much nailed the permutations part of it, so I just stole copied that part of the formula. Removing the duplicates was tougher, but I managed. Performance wise, this is a terrible function. 3 and 4 work ok, 5 items take 20 seconds, and I haven't tried anything bigger. BYROW is definitely an improvement.

Book1
ABCDEFGHIJKL
1ValuesRepeatRepeat
2Dog0DogCatBird1DogDogDog
3CatDogBirdCatDogDogCat
4BirdCatDogBirdDogDogBird
5CatBirdDogDogCatDog
6BirdDogCatDogCatCat
7BirdCatDogDogCatBird
8DogBirdDog
9DogBirdCat
10DogBirdBird
11CatDogDog
12CatDogCat
13CatDogBird
14CatCatDog
15CatCatCat
16CatCatBird
17CatBirdDog
18CatBirdCat
19CatBirdBird
20BirdDogDog
21BirdDogCat
22BirdDogBird
23BirdCatDog
24BirdCatCat
25BirdCatBird
26BirdBirdDog
27BirdBirdCat
28BirdBirdBird
Sheet16
Cell Formulas
RangeFormula
D2:F7,J2:L28D2=LET(aa,$A2:$A10,a,FILTER(aa,aa<>""),r,ROWS(a),b,MID(BASE(SEQUENCE(r^r,,0),r,r),SEQUENCE(,r),1)+1,sq,SEQUENCE(r),s,MOD(SMALL(b+SEQUENCE(r^r,,10,10),SEQUENCE(r^r,r)),10),m,MMULT(s,10^sq),ix,INDEX(a,b),IF(C2=1,ix,FILTER(ix,m=SUM(sq*(10^sq)))))
Dynamic array formulas.
As much as I like making formulas like this, they all paddle in comparison performance wise to VBA. I've saved some coffee to do it. It uses recursion which makes my head hurt. But yeah, like about 7 makes the formula take a long time. Even using Fluff's formula.
 
Upvote 0

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Allowing duplicates, isn't it a simple?

=LET(a,A2:A4,r,ROWS(a),INDEX(a,MOD(SEQUENCE(r^r,,0)/r^SEQUENCE(,r,r-1,-1),r)+1))

Or am I missing something?

We could filter duplicates like this:

=LET(a,A2:A4,r,ROWS(a), n,INT(MOD(SEQUENCE(r^r,,0)/r^SEQUENCE(,r,r-1,-1),r)+1),INDEX(a,FILTER(n,1=BYROW(n,LAMBDA(rw,MAX(FREQUENCY(rw,SEQUENCE(r))))))))

... but I'm thinking there must be a slicker way.
 
Upvote 0

Forum statistics

Threads
1,214,983
Messages
6,122,591
Members
449,089
Latest member
Motoracer88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top