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
860
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.
 
Super challenge, great Py/Copilot solution, latest YT of @MrExcel
The concept is simple.
Max nr of combinations to solve this problem is 6435.
To calculate them will use my Random COMBIN function RCOMBIN
n=15, c (nr. chosen) will be considered possible to have 2 values c1=floor(target val/AVERAGE(all values) ,1) and c2=c1+1
We check all combinations sums and we extract the one closest to the target amount.

CMBSUM(a,s) Combination Sum a: vector of sums ; s: target sum calls RCOMBIN
Excel Formula:
=LAMBDA(a, s,
    LET(
        n, ROWS(a),
        c, FLOOR(s / AVERAGE(a), 1),
        r, VSTACK(RCOMBIN(n, c), RCOMBIN(n, c + 1)),
        m, MAP(r, LAMBDA(x, ABS(SUM(INDEX(a, TEXTSPLIT(x, ","))) - s))),
        i, INDEX(a, TEXTSPLIT(XLOOKUP(0, m, r, , 1), , ",")),
        u, SUM(i),
        VSTACK("Err", ABS(u - s), i, "Tot", u)
    )
)

RCOMBIN(n,c) Random COMBIN n: number ; c: nr. chosen (like COMBIN arg)
Excel Formula:
=LAMBDA(n, c,
    LET(
        k, 20 * COMBIN(n, c),
        UNIQUE(SORT(MAP(SEQUENCE(k), LAMBDA(x, TEXTJOIN(",", , TEXT(SORT(TAKE(SORTBY(SEQUENCE(n), RANDARRAY(n)), c)), REPT(0, LEN(n))))))))
    )
)
Book2
ABCDEFGHIJKLMNOP
1
2
3max comb for 15 elem are 6435
4s154967.28s267045.7s365332.58
5=COMBIN(15,SEQUENCE(15))a
6156920.73ErrErrErr
71058586.86000
84555483.676920.736920.736920.73
913658741.455483.678586.865483.67
1030036435.268741.455483.676435.26
1150057497.847497.848741.457497.84
1264356937.266937.267497.846937.26
1364354616.794616.795590.454274.05
1450054274.058400.462592.485590.45
1530035590.456369.088400.462592.48
1613652592.48Tot3318.243318.24
174558400.4654967.289913.526369.08
181053318.24Tot9913.52
19156369.0867045.7Tot
2019913.5265332.58
21
22H6K6N6
23=CMBSUM(E6:E20,H4)=CMBSUM(E6:E20,K4)=CMBSUM(E6:E20,N4)
24
25
Sheet1
Cell Formulas
RangeFormula
A5A5=FORMULATEXT(B6)
B6:B20B6=COMBIN(15,SEQUENCE(15))
H6:H17H6=CMBSUM(E6:E20,H4)
K6:K19K6=CMBSUM(E6:E20,K4)
N6:N20N6=CMBSUM(E6:E20,N4)
H23,K23,N23H23=FORMULATEXT(H6)
Dynamic array formulas.


RCOMBIN can return up to 50000 unique combinations, Other function can deal with lot more. Uses same concept but with an iterative algorithm. Will explain all these concepts some other time.
Book2
ABCDEFGHIJK
1
2RCOMBIN examples
3
4=RCOMBIN(12,7)=RCOMBIN(7,4)
501,02,03,04,05,06,071,2,3,4
601,02,03,04,05,06,08check val.1,2,3,5check val.
701,02,03,04,05,06,09=COMBIN(12,7)1,2,3,6=COMBIN(7,4)
801,02,03,04,05,06,107921,2,3,735
901,02,03,04,05,06,111,2,4,5
1001,02,03,04,05,06,12=ROWS(B5#)1,2,4,6=ROWS(G5#)
1101,02,03,04,05,07,087921,2,4,735
1201,02,03,04,05,07,091,2,5,6
1301,02,03,04,05,07,10=ROWS(UNIQUE(B5#))1,2,5,7=ROWS(UNIQUE(G5#))
1401,02,03,04,05,07,117921,2,6,735
1501,02,03,04,05,07,121,3,4,5
1601,02,03,04,05,08,091,3,4,6
1701,02,03,04,05,08,101,3,4,7
1801,02,03,04,05,08,111,3,5,6
1901,02,03,04,05,08,121,3,5,7
2001,02,03,04,05,09,101,3,6,7
2101,02,03,04,05,09,111,4,5,6
2201,02,03,04,05,09,121,4,5,7
2301,02,03,04,05,10,111,4,6,7
2401,02,03,04,05,10,121,5,6,7
2501,02,03,04,05,11,122,3,4,5
2601,02,03,04,06,07,082,3,4,6
2701,02,03,04,06,07,092,3,4,7
2801,02,03,04,06,07,102,3,5,6
2901,02,03,04,06,07,112,3,5,7
3001,02,03,04,06,07,122,3,6,7
3101,02,03,04,06,08,092,4,5,6
3201,02,03,04,06,08,102,4,5,7
3301,02,03,04,06,08,112,4,6,7
3401,02,03,04,06,08,122,5,6,7
3501,02,03,04,06,09,103,4,5,6
3601,02,03,04,06,09,113,4,5,7
3701,02,03,04,06,09,123,4,6,7
3801,02,03,04,06,10,113,5,6,7
3901,02,03,04,06,10,124,5,6,7
4001,02,03,04,06,11,12
4101,02,03,04,07,08,09
Sheet2
Cell Formulas
RangeFormula
G4,I13,D13,I10,D10,I7,D7,B4G4=FORMULATEXT(G5)
B5:B796B5=RCOMBIN(12,7)
G5:G39G5=RCOMBIN(7,4)
D8D8=COMBIN(12,7)
I8I8=COMBIN(7,4)
D11,I11D11=ROWS(B5#)
D14,I14D14=ROWS(UNIQUE(B5#))
Dynamic array formulas.
 
Another cool PQ alternative challenge:
Other use of RCOMBIN (previous post)
Book1
ABCDEFGHIJKLMNOPQRSTU
1
2IDCost=LET(c,C3:C7,n,ROWS(c),x,DROP(REDUCE(0,SEQUENCE(n),LAMBDA(v,i,VSTACK(v,RCOMBIN(n,i)))),1),HSTACK(x,MAP(x,LAMBDA(x,SUM(INDEX(c,TEXTSPLIT(x,",")))))))
31212
42323
53535
64747
7511511
81,25
91,37
101,49
111,513
122,38
132,410
142,514
153,412
163,516
174,518
181,2,310
191,2,412
201,2,516
211,3,414
221,3,518
231,4,520
242,3,415
252,3,519
262,4,521
273,4,523
281,2,3,417
291,2,3,521
301,2,4,523
311,3,4,525
322,3,4,526
331,2,3,4,528
34
Sheet1
Cell Formulas
RangeFormula
E2E2=FORMULATEXT(E3)
E3:F33E3=LET(c,C3:C7,n,ROWS(c),x,DROP(REDUCE(0,SEQUENCE(n),LAMBDA(v,i,VSTACK(v,RCOMBIN(n,i)))),1),HSTACK(x,MAP(x,LAMBDA(x,SUM(INDEX(c,TEXTSPLIT(x,",")))))))
Dynamic array formulas.


Other approach using PA (permutations with repetitions, correspond to PERMUTATIONA)
PA(n,c)
Excel Formula:
=LAMBDA(n,c,MOD(QUOTIENT(SEQUENCE(n^c)-1,n^(c-SEQUENCE(,c))),n)+1)
Book1
ABCDEFGHIJKLMNO
1single cell
2=PA(5,5)=UNIQUE(BYROW(PA(5,5),LAMBDA(x,TEXTJOIN(",",,UNIQUE(SORT(x,,,1),1)))))
3111111
4111121,2
5111131,3
6111141,4
7111151,5
8111211,2,3
9111221,2,4
10111231,2,5
11111241,3,4
12111251,3,5
13111311,4,5
14111321,2,3,4
15111331,2,3,5
16111341,2,4,5
17111351,3,4,5
18111411,2,3,4,5
19111422
20111432,3
21111442,4
22111452,5
23111512,3,4
24111522,3,5
25111532,4,5
26111542,3,4,5
27111553
28112113,4
29112123,5
30112133,4,5
31112144
32112154,5
33112215
3411222
Sheet2
Cell Formulas
RangeFormula
B2,H2B2=FORMULATEXT(B3)
B3:F3127B3=PA(5,5)
H3:H33H3=UNIQUE(BYROW(PA(5,5),LAMBDA(x,TEXTJOIN(",",,UNIQUE(SORT(x,,,1),1)))))
Dynamic array formulas.
 

Forum statistics

Threads
1,223,236
Messages
6,170,912
Members
452,366
Latest member
TePunaBloke

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