ARRANGEMENTS

=ARRANGEMENTS(a,t,c)

a
array
t
type argument,"pa", permutations with repetitions; "p" permutations w/o repet. ; "ca", combinations with repet. ; "c", combinations w/o repet.
c
number_chosen

Combinatorics study, permutations, combinations w or w/o repetitions, all in one function.

Xlambda

Well-known Member
Joined
Mar 8, 2021
Messages
832
Office Version
  1. 365
Platform
  1. Windows
ARRANGEMENTS Extracts all permutations or combinations, with or without repetitions, of all elements of an array, by a number chosen.
Study inspired by latest MrExcel's YT (15aug21) Excel All Combinations Using Power Query - 2424
This is the first draft I came with, of course, the recursion way deserves first place. There are 2 other approaches, one using AFUSBYROW/BYCOL functions, and the other using new functions.
Calls 3 tool lambdas, 1 non recursive and 2 recursive (no risk to reach limitations, recursion takes place on the "short" direction, the columns one) . The recursive ones were created with ease using the ARF recursive kits concepts. Also, both recursive ones can be used as standalone lambdas, T_P is filtering an array by rows with no dups, and T_CA is filtering an array by rows that are in ascending order. The challenge was to do this by columns iterations and not by rows iteration (too many).
For ARRANGEMENTS function we use them to create the index patterns for extracting array's elements as are required. T_PA (non-recursive) creates the index pattern for permutations with repetitions, Once we have that, T_P (recursive) creates the index pattern for permutations without repetitions T_P(T_PA), T_CA creates the patterns for combinations with repetitions T_CA(T_PA), and for combinations without repetitions we use the same T_CA but this time, as input, we use T_P, T_CA(T_P). Calculation time for arrays having 1M rows 1 to 3 seconds.
T_PA(n,c)=LAMBDA(n,c,MOD(ROUNDUP(SEQUENCE(n^c)/n^(c-SEQUENCE(,c)),0)-1,n)+1) where n: number ; c: number chosen
T_P(a,[ai],[ i ] )=LAMBDA(a,[ai],[ i ],LET(n,COLUMNS(a),j,IF(i="",n,i),x,INDEX(a,,j),IF(j=0,FILTER(a,MMULT(ai,SEQUENCE( n)^0)=n),T_P(a,ai+(x=a),j-1)))) !! recursive !!
T_CA(a,[ai],[ i ])=LAMBDA(a,[ai],[ i ],LET(n,COLUMNS(a),j,IF(i="",1,i),aj,IF(ai="",1,ai),x,INDEX(a,,j),IF(j=n,FILTER(a,aj),T_CA(a,aj*(x<=INDEX(a,,j+1)),j+1))))
!! recursive !! where a:array, ai,i,omitted

Excel Formula:
=LAMBDA(a,t,c,
    IF(AND(t<>{"p","pa","c","ca"}),"check type",
      LET(k,MAX(1,c),x,AFLAT(a),n,ROWS(x),IF(AND(OR(t={"p","c"}),k>n),"nr chosen>n !",LET(y,T_PA(n,k),
       SWITCH(t,"pa",INDEX(x,y),"p",INDEX(x,T_P(y)),"ca",INDEX(x,T_CA(y)),"c",LET(z,T_P(y),w,T_CA(z),INDEX(x,w))))))
    )
)
LAMBDA 1.1.1.xlsx
ABCDEFGHIJKLMNO
1Introduction: Combinatorics Excel functions:
21,) PERMUTATIONA Returns the number of permutations for a given number of objects (with repetitions) that can be selected from the total objects.
3 - PERMUTATIONA(number, number-chosen); "nc" or "c" (number chosen) can be >=n (number of elements/objects) ; order is important; PA=n^nc
42.) PERMUT Returns the number of permutations for a given number of objects (no repetitions) that can be selected from the total objects.
5- PERMUT(number, number_chosen); if nc>n returns #NUM! error; also called arrangements; order is important ;P=n!/(n-nc)!
63.) COMBINA Returns the number of combinations (with repetitions) for a given number of items.
7- COMBINA(number, number_chosen) ; nc can be > n; order is not important; CA=(n+nc-1)!/(nc!*(n-1)!)
84.) COMBIN Returns the number of combinations (no repetitions) for a given number of items.
9- COMBINA(number, number_chosen) ; nc can be > n; order is not important; C=P/nc! or C=n!/(nc!*(n-nc)!)
10What ARRANGEMENTS does is "printing" or extracting all this numbers of permutations or combinations, given the object array "a" and the number_chosen "c",
11for all types "t" above : "pa" for 1.) , "p" for 2.) , "ca" for 3.) ,"c" for 4.)
12
13input n:7Table of values returned by all functions for an array of n objects ,nc [1,10]
14
15function\nc12345678910
161,) PERMUTATIONA749343240116807117649823543576480140353607282475249
172,) PERMUT742210840252050405040#NUM!#NUM!#NUM!
183.) COMBINA728842104629241716300350058008
194.) COMBIN72135352171#NUM!#NUM!#NUM!
20
21Note:It is easy to run out of "printing" real estate in an excel spreadsheet for small arrays of objects
22
ARR post 1
Cell Formulas
RangeFormula
C15:L15C15=SEQUENCE(,10)
C16:L16C16=PERMUTATIONA(C13,C15#)
C17:L17C17=PERMUT(C13,C15#)
C18:L18C18=COMBINA(C13,C15#)
C19:L19C19=COMBIN(C13,C15#)
Dynamic array formulas.
 
Upvote 0
Please read all the posts first, there are all explanations there. The limits are Excel's limits, post #5
If you read the posts, there is no recursion limit because the direction of iterations is by the nr of columns not nr. of rows.
 
Thank you for explaining this. I think I found the right post.
My Excel returns a zero (i.e. fails) when I go up to an array of 7. i.e. on ARRANGEMENTS({1,2,3,4,5,6,7}, "c", 7)
 
You do not need any function or calculator to calculate that.
COMBIN(7,7)=1
Anyhow ARRANGEMENTS works fine in all its designed aspects and in this case returns the only possible combination 1,2,3,4,5,6,7
Book1
ABCDEFGHI
1
2
31234567
4
5=ARRANGEMENTS(B3:H3,"c",7)
61234567
7
8
Sheet1
Cell Formulas
RangeFormula
B5B5=FORMULATEXT(B6)
B6:H6B6=ARRANGEMENTS(B3:H3,"c",7)
Dynamic array formulas.
 
Here are the other lists, Permutations (w and w/o repetitions) and Combinations with repetitions, for same array of 7 elementes and a nr.chosen of 7
Could be something with your computer that something makes it toooo slooooow.
Book1
ABCDEFGHIJKLMNOPQRSTUVWXYZAA
11234567
2
3=PERMUTATIONA(7,7)=ROWS(B7#)=PERMUT(7,7)=ROWS(K7#)=COMBINA(7,7)=ROWS(T7#)
48235438235435040504017161716
5
6=ARRANGEMENTS(B1#,"pa",7)=ARRANGEMENTS(B1#,"p",7)=ARRANGEMENTS(B1#,"ca",7)
7111111112345671111111
8111111212345761111112
9111111312346571111113
10111111412346751111114
11111111512347561111115
12111111612347651111116
13111111712354671111117
14111112112354761111122
15111112212356471111123
16111112312356741111124
17111112412357461111125
18111112512357641111126
19111112612364571111127
20111112712364751111133
21111113112365471111134
22111113212365741111135
23111113312367451111136
24111113412367541111137
25111113512374561111144
26111113612374651111145
27111113712375461111146
28111114112375641111147
29111114212376451111155
30111114312376541111156
31111114412435671111157
32111114512435761111166
33111114612436571111167
34111114712436751111177
35111115112437561111222
36111115212437651111223
37111115312453671111224
38111115412453761111225
39111115512456371111226
40111115612456731111227
41111115712457361111233
42111116112457631111234
43111116212463571111235
44111116312463751111236
45111116412465371111237
46111116512465731111244
47111116612467351111245
48111116712467531111246
49111117112473561111247
50111117212473651111255
51111117312475361111256
52111117412475631111257
53111117512476351111266
54111117612476531111267
55111117712534671111277
56111121112534761111333
57111121212536471111334
58111121312536741111335
59111121412537461111336
60111121512537641111337
61111121612543671111344
62111121712543761111345
63111122112546371111346
64111122212546731111347
65111122312547361111355
66111122412547631111356
67111122512563471111357
68111122612563741111366
69111122712564371111367
70111123112564731111377
71111123212567341111444
72111123312567431111445
73111123412573461111446
74111123512573641111447
75111123612574361111455
76111123712574631111456
77111124112576341111457
78111124212576431111466
79111124312634571111467
80111124412634751111477
Sheet2
Cell Formulas
RangeFormula
B1:H1B1=SEQUENCE(,7)
B3,T6,K6,B6,O3,G3,X3,T3,K3B3=FORMULATEXT(B4)
B4B4=PERMUTATIONA(7,7)
G4G4=ROWS(B7#)
K4K4=PERMUT(7,7)
O4,X4O4=ROWS(K7#)
T4T4=COMBINA(7,7)
B7:H823549B7=ARRANGEMENTS(B1#,"pa",7)
K7:Q5046K7=ARRANGEMENTS(B1#,"p",7)
T7:Z1722T7=ARRANGEMENTS(B1#,"ca",7)
Dynamic array formulas.
 
Yes, I think it’s something wrong with my Excel. I’m on a Mac, and a combination, without repetitions, on a set of length 7, choosing 7 elements kills it. Choosing 6 elements works fine (about 10 seconds).
 
Cool challenge MrExcel latest YT : World Cup Group Stage Tiebreaker Odds - 2531
Single cell formula alternative to (10:49) Generating 729 input cells
Formula alternative using only T_PA (permutations with repetitions index patterns formula - 1)
Arrangements.xlsx
ABCDEFGHIJKLM
1
2=T_PA(3,6)-1=BYROW(T_PA(3,6)-1,LAMBDA(x,TEXTJOIN("-",,x)))
30000000-0-0-0-0-0
40000010-0-0-0-0-1checking rows
50000020-0-0-0-0-2=PERMUTATIONA(3,6)
60000100-0-0-0-1-0729
70000110-0-0-0-1-1
80000120-0-0-0-1-2=ROWS(B3#)
90000200-0-0-0-2-0729
100000210-0-0-0-2-1
110000220-0-0-0-2-2=ROWS(I3#)
120001000-0-0-1-0-0729
130001010-0-0-1-0-1
140001020-0-0-1-0-2
150001100-0-0-1-1-0
160001110-0-0-1-1-1
170001120-0-0-1-1-2
180001200-0-0-1-2-0
190001210-0-0-1-2-1
200001220-0-0-1-2-2
210002000-0-0-2-0-0
220002010-0-0-2-0-1
230002020-0-0-2-0-2
240002100-0-0-2-1-0
250002110-0-0-2-1-1
260002120-0-0-2-1-2
270002200-0-0-2-2-0
280002210-0-0-2-2-1
290002220-0-0-2-2-2
300010000-0-1-0-0-0
310010010-0-1-0-0-1
320010020-0-1-0-0-2
330010100-0-1-0-1-0
340010110-0-1-0-1-1
350010120-0-1-0-1-2
360010200-0-1-0-2-0
370010210-0-1-0-2-1
380010220-0-1-0-2-2
390011000-0-1-1-0-0
400011010-0-1-1-0-1
410011020-0-1-1-0-2
420011100-0-1-1-1-0
430011110-0-1-1-1-1
440011120-0-1-1-1-2
450011200-0-1-1-2-0
460011210-0-1-1-2-1
470011220-0-1-1-2-2
480012000-0-1-2-0-0
490012010-0-1-2-0-1
500012020-0-1-2-0-2
510012100-0-1-2-1-0
520012110-0-1-2-1-1
530012120-0-1-2-1-2
540012200-0-1-2-2-0
550012210-0-1-2-2-1
560012220-0-1-2-2-2
570020000-0-2-0-0-0
580020010-0-2-0-0-1
590020020-0-2-0-0-2
600020100-0-2-0-1-0
610020110-0-2-0-1-1
620020120-0-2-0-1-2
630020200-0-2-0-2-0
640020210-0-2-0-2-1
650020220-0-2-0-2-2
660021000-0-2-1-0-0
670021010-0-2-1-0-1
680021020-0-2-1-0-2
690021100-0-2-1-1-0
700021110-0-2-1-1-1
710021120-0-2-1-1-2
720021200-0-2-1-2-0
730021210-0-2-1-2-1
740021220-0-2-1-2-2
750022000-0-2-2-0-0
760022010-0-2-2-0-1
770022020-0-2-2-0-2
780022100-0-2-2-1-0
790022110-0-2-2-1-1
800022120-0-2-2-1-2
810022200-0-2-2-2-0
820022210-0-2-2-2-1
830022220-0-2-2-2-2
840100000-1-0-0-0-0
850100010-1-0-0-0-1
860100020-1-0-0-0-2
870100100-1-0-0-1-0
880100110-1-0-0-1-1
890100120-1-0-0-1-2
900100200-1-0-0-2-0
Bill YT
Cell Formulas
RangeFormula
B2,K11,K8,K5,I2B2=FORMULATEXT(B3)
B3:G731B3=T_PA(3,6)-1
I3:I731I3=BYROW(T_PA(3,6)-1,LAMBDA(x,TEXTJOIN("-",,x)))
K6K6=PERMUTATIONA(3,6)
K9K9=ROWS(B3#)
K12K12=ROWS(I3#)
Dynamic array formulas.
 

Forum statistics

Threads
1,214,797
Messages
6,121,629
Members
449,041
Latest member
Postman24

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