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
Example inspired by today's (12-Dec-21) MrExcel YT: Random Combination Of PBPBBP - 2448
The challenge is: "Jon wants to generate 6-letter sequences using only the letters B and P.
So, for example: BBBBBB, PPPPPP, BPBPPB, BBBPPP, and so on. There are 64 such combinations and Bill shows you one way to solve this use BASE and SUBSTITUTE.
"
Check the video please, you will find out that using of BASE function is a super cool trick.
Also, this can be solved with ARRANGEMENTS function. Other functions on minisheet ATEXTJOIN
LAMBDA 1.2.1.xlsx
ABCDEFGHIJKLMNO
1LettersNumber chosen
2BP6
3
41. Why there are 64 possible "combinations" (permutations with repetition) ?
5Because PERMUTATIONA(2,6)=64
6=PERMUTATIONA(2,6)
764
8For example, for 3 letters total nr. will be PERMUTATIONA(3,6)=729
9=PERMUTATIONA(3,6)
10729
11Now , the only thing to do is to concatenate the columns(BYROW)
12=ARRANGEMENTS(A2:B2,"pa",6)=BYROW(A13#,LAMBDA(a,CONCAT(a)))
13BBBBBBBBBBBB
14BBBBBPBBBBBPOr a single cell using ATEXTJOIN
15BBBBPBBBBBPB=ATEXTJOIN(ARRANGEMENTS(A2:B2,"pa",6),"")
16BBBBPPBBBBPPBBBBBB
17BBBPBBBBBPBBBBBBBPchecks
18BBBPBPBBBPBPBBBBPB=ROWS(A13#)=ROWS(J16#)
19BBBPPBBBBPPBBBBBPP6464
20BBBPPPBBBPPPBBBPBB
21BBPBBBBBPBBBBBBPBP=INDEX(J16#,1)
22BBPBBPBBPBBPBBBPPBfirst value :BBBBBB
23BBPBPBBBPBPBBBBPPPlast value:PPPPPP
24BBPBPPBBPBPPBBPBBB=INDEX(J16#,64)
25BBPPBBBBPPBBBBPBBP
26BBPPBPBBPPBPBBPBPB
27BBPPPBBBPPPBBBPBPPOther functions
28BBPPPPBBPPPPBBPPBBATEXTJOIN
29BPBBBBBPBBBBBBPPBP
30BPBBBPBPBBBPBBPPPB
31BPBBPBBPBBPBBBPPPP
32BPBBPPBPBBPPBPBBBB
33BPBPBBBPBPBBBPBBBP
34BPBPBPBPBPBPBPBBPB
35BPBPPBBPBPPBBPBBPP
36BPBPPPBPBPPPBPBPBB
37BPPBBBBPPBBBBPBPBP
38BPPBBPBPPBBPBPBPPB
39BPPBPBBPPBPBBPBPPP
40BPPBPPBPPBPPBPPBBB
41BPPPBBBPPPBBBPPBBP
42BPPPBPBPPPBPBPPBPB
43BPPPPBBPPPPBBPPBPP
44BPPPPPBPPPPPBPPPBB
45PBBBBBPBBBBBBPPPBP
46PBBBBPPBBBBPBPPPPB
47PBBBPBPBBBPBBPPPPP
48PBBBPPPBBBPPPBBBBB
49PBBPBBPBBPBBPBBBBP
50PBBPBPPBBPBPPBBBPB
51PBBPPBPBBPPBPBBBPP
52PBBPPPPBBPPPPBBPBB
53PBPBBBPBPBBBPBBPBP
54PBPBBPPBPBBPPBBPPB
55PBPBPBPBPBPBPBBPPP
56PBPBPPPBPBPPPBPBBB
57PBPPBBPBPPBBPBPBBP
58PBPPBPPBPPBPPBPBPB
59PBPPPBPBPPPBPBPBPP
60PBPPPPPBPPPPPBPPBB
61PPBBBBPPBBBBPBPPBP
62PPBBBPPPBBBPPBPPPB
63PPBBPBPPBBPBPBPPPP
64PPBBPPPPBBPPPPBBBB
65PPBPBBPPBPBBPPBBBP
66PPBPBPPPBPBPPPBBPB
67PPBPPBPPBPPBPPBBPP
68PPBPPPPPBPPPPPBPBB
69PPPBBBPPPBBBPPBPBP
70PPPBBPPPPBBPPPBPPB
71PPPBPBPPPBPBPPBPPP
72PPPBPPPPPBPPPPPBBB
73PPPPBBPPPPBBPPPBBP
74PPPPBPPPPPBPPPPBPB
75PPPPPBPPPPPBPPPBPP
76PPPPPPPPPPPPPPPPBB
77PPPPBP
78PPPPPB
79PPPPPP
80
ARRG ex 1
Cell Formulas
RangeFormula
A6,M21,L18,N18,J15,A12,H12,A9A6=FORMULATEXT(A7)
A7A7=PERMUTATIONA(2,6)
A10A10=PERMUTATIONA(3,6)
A13:F76A13=ARRANGEMENTS(A2:B2,"pa",6)
H13:H76H13=BYROW(A13#,LAMBDA(a,CONCAT(a)))
J16:J79J16=ATEXTJOIN(ARRANGEMENTS(A2:B2,"pa",6),"")
L19L19=ROWS(A13#)
N19N19=ROWS(J16#)
M22M22=INDEX(J16#,1)
M23M23=INDEX(J16#,64)
M24M24=FORMULATEXT(M23)
Dynamic array formulas.
 
Study of using BASE function, as feasible alternative for creating arrays of permutations with repetitions, replacing T_PA function concept, the function that is triggered by ARRANGEMENTS when this this type of permutation is needed.
LAMBDA 1.2.1.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABAC
1Using of BASE function concept vs. T_PA function. Part 1.
2As we know, the t="pa" (type) argument in ARRANGEMENTS(a,t,c) is triggering T_PA(n,c) function
3
4total arrangements
5LettersNr. chosen=COLUMNS(A6:B6)^C6To avoid using nested SUBSTITUTE functions there are 2 approaches
6BP532- calling custom made function AREPLACE (deals with nested substitute's)
7Note: For shorter formula , replaced permutationa(n,c) with n^c- creating an array of the arrangements "indexes"T_PA returns indexes array
8=BASE(SEQUENCE(COLUMNS(A6:B6)^C6)-1,COLUMNS(A6:B6),C6)AREPLACEby default, no AREPLACE needed
9=SUBSTITUTE(SUBSTITUTE(A10#,0,A6),1,B6)=AREPLACE(A10#,{0,1},A6:B6)=MID(A10#,SEQUENCE(,C6),1)+1=T_PA(2,5)=INDEX(A6:B6,R10#)
1000000BBBBBBBBBB1111111111BBBBB
1100001BBBBPBBBBP1111211112BBBBP
1200010BBBPBBBBPB1112111121BBBPB
1300011BBBPPBBBPP1112211122BBBPP
1400100BBPBBBBPBB1121111211BBPBB
1500101BBPBPBBPBP1121211212BBPBP
1600110BBPPBBBPPB1122111221BBPPB
1700111BBPPPBBPPP1122211222BBPPP
1801000BPBBBBPBBB1211112111BPBBB
1901001BPBBPBPBBP1211212112BPBBP
2001010BPBPBBPBPB1212112121BPBPB
2101011BPBPPBPBPP1212212122BPBPP
2201100BPPBBBPPBB1221112211BPPBB
2301101BPPBPBPPBP1221212212BPPBP
2401110BPPPBBPPPB1222112221BPPPB
2501111BPPPPBPPPP1222212222BPPPP
2610000PBBBBPBBBB2111121111PBBBB
2710001PBBBPPBBBP2111221112PBBBP
2810010PBBPBPBBPB2112121121PBBPB
2910011PBBPPPBBPP2112221122PBBPP
3010100PBPBBPBPBB2121121211PBPBB
3110101PBPBPPBPBP2121221212PBPBP
3210110PBPPBPBPPB2122121221PBPPB
3310111PBPPPPBPPP2122221222PBPPP
3411000PPBBBPPBBB2211122111PPBBB
3511001PPBBPPPBBP2211222112PPBBP
3611010PPBPBPPBPB2212122121PPBPB
3711011PPBPPPPBPP2212222122PPBPP
3811100PPPBBPPPBB2221122211PPPBB
3911101PPPBPPPPBP2221222212PPPBP
4011110PPPPBPPPPB2222122221PPPPB
4111111PPPPPPPPPP2222222222PPPPP
42
ARRG ex 2
Cell Formulas
RangeFormula
E5,X9,I9,L9,R9,C9E5=FORMULATEXT(E6)
E6E6=COLUMNS(A6:B6)^C6
A8A8=FORMULATEXT(A10)
A10:A41A10=BASE(SEQUENCE(COLUMNS(A6:B6)^C6)-1,COLUMNS(A6:B6),C6)
C10:C41C10=SUBSTITUTE(SUBSTITUTE(A10#,0,A6),1,B6)
I10:I41I10=AREPLACE(A10#,{0,1},A6:B6)
L10:P41L10=MID(A10#,SEQUENCE(,C6),1)+1
R10:V41R10=T_PA(2,5)
X10:AB41X10=INDEX(A6:B6,R10#)
Dynamic array formulas.
 
LAMBDA 1.2.1.xlsx
ABCDEFGHIJKLMNOPQRSTUV
1Using of BASE function concept vs. T_PA function. Part 2.
2We have seen that BASE function can have good premises to replace T_PA(n,c) function, or not?
3T_PA(n,c)=LAMBDA(n,c,MOD(ROUNDUP(SEQUENCE(n^c)/n^(c-SEQUENCE(,c)),0)-1,n)+1)
4Also we have seen that, to avoid nested SUBSTITUTE functions, it is better to create array of "indexes" concept,
5that avoids calling other function AREPLACE
6Designing T_PAB(n,c) function using BASE for a simple scenario n=2,c=3 (n<c scenario)
7
8step 1. BASE(SEQUENCE(n^c)-1,n,c)
9=BASE(SEQUENCE(2^3)-1,2,3)
10000
11001step 2. expanding values tostep 3. for corect indexesstep 4. The function
12010an array of indexeswe have to add 1
13011=MID(A10#,SEQUENCE(,3),1)=C14#+1=LAMBDA(n,c,LET(b,BASE(SEQUENCE(n^c)-1,n,c),MID(b,SEQUENCE(,c),1)+1))(2,3)
14100000111111
15101001112112
16110010121121
17111011122122
18100211211
19101212212
20110221221
21111222222
22
23 T_PAB(n,c)=LAMBDA(n,c,LET(b,BASE(SEQUENCE(n^c)-1,n,c),MID(b,SEQUENCE(,c),1)+1))
24
25=T_PAB(2,3)=T_PA(2,3)
26111111
27112112
28121121
29122122
30211211
31212212
32221221
33222222
34
35So far so good, both functions return same outcome,on the next post will cover scenarios where n>c
36
ARRG ex 3
Cell Formulas
RangeFormula
A9,H25,C25,M13,C13,H13A9=FORMULATEXT(A10)
A10:A17A10=BASE(SEQUENCE(2^3)-1,2,3)
C14:E21C14=MID(A10#,SEQUENCE(,3),1)
H14:J21H14=C14#+1
M14:O21M14=LAMBDA(n,c,LET(b,BASE(SEQUENCE(n^c)-1,n,c),MID(b,SEQUENCE(,c),1)+1))(2,3)
C26:E33C26=T_PAB(2,3)
H26:J33H26=T_PA(2,3)
Dynamic array formulas.
 
LAMBDA 1.2.1.xlsx
ABCDEFGHIJKLMNOPQRSTU
1Using of BASE function concept vs. T_PA function. Part 3.(n>c scenario)
2Checking T_PAB(n,c) function using BASE for n=13,c=3The reason T_PAB returns errors is because BASE, when radix argument n>10, returns letters
3- total possibilities n^c=2197This problem can be addressed easily with AREPLACE, but this is exactly what we wanted to avoid.
4Conclusion: T_PA is better for covering both scenarios.
5So T_PA will stay, and on following posts will cover what I have stated since day one, alternatives for the recursive ones T_P and T_CA.
6"There are 2 other approaches, one using AFUSBYROW/BYCOL functions, and the other using new functions." (lambda helper functions)
7
8=T_PAB(13,3)=T_PA(13,3)=BASE(SEQUENCE(13^3)-1,13,3)=MID(I9#,SEQUENCE(,3),1)=K9#+1
9111111000000111checking
10112112001001112=ROWS(A9#)
111131130020021132197
12114114003003114=ROWS(E9#)
131151150040041152197
14116116005005116=ROWS(I9#)
151171170060061172197
16118118007007118=ROWS(O9#)
171191190080081192197
18111011100090091110
1911#VALUE!111100A00A11#VALUE!
2011#VALUE!111200B00B11#VALUE!
2111#VALUE!111300C00C11#VALUE!
22121121010010121
23122122011011122
24123123012012123
25124124013013124
26125125014014125
27126126015015126
28127127016016127
29128128017017128
30129129018018129down to
311210121001901912102197 rows
3212#VALUE!121101A01A12#VALUE!↓↓↓↓
3312#VALUE!121201B01B12#VALUE!
3412#VALUE!121301C01C12#VALUE!
ARRG ex 4
Cell Formulas
RangeFormula
D3D3=13^3
A8,S10,S12,S14,S16,I8,K8,O8,E8A8=FORMULATEXT(A9)
A9:C2205A9=T_PAB(13,3)
E9:G2205E9=T_PA(13,3)
I9:I2205I9=BASE(SEQUENCE(13^3)-1,13,3)
K9:M2205K9=MID(I9#,SEQUENCE(,3),1)
O9:Q2205O9=K9#+1
S11S11=ROWS(A9#)
S13S13=ROWS(E9#)
S15S15=ROWS(I9#)
S17S17=ROWS(O9#)
Dynamic array formulas.
 
When I first started this thread, I've said: "There are 2 other approaches, one using AFUSBYROW/BYCOL functions, and the other using new functions." Here are the other approaches.
Recap: Permutations
As we know PERMUTATIONA(n,c) calculates all permutations with repetitions of "n" by nr. chosen "c", and PERMUT(n,c) calculates the number of permutations without repetitions.
T_PA(n,c) is a function we created to "print" an array "a" of all these permutations with repetitions. Is simple and non-recursive.
T_P(a) is a recursive function filters the rows of "a" that have repetitions (dups), will keep only the rows with no dups. To check these results, rows(T_P(a))=PERMUT(n,c), the same "n" and "c" that created "a".
T_PN(a) will replace the recursive one using New REDUCE and BYROW.
T_PF(a) will replace the recursive one with other existing Functions AFUSBYROW and AHCLEAN.
Note: All functions T_P(a),T_PN(a),T_PF(a), can be used as standalone functions that will be capable to filter any array "a" for rows w/o dups, not only the array "a" created by T_PA(n,c)
LAMBDA 1.2.1.xlsx
ABCDEFGHIJKLMNOPQRSTUVWX
1Concept of T_P(a), T_PN(a)
2
3 T_P(a)=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))))
4
5clm 1=aclm 2=aclm 3=aif we sum all these arrayssumming by rows
6a=A7:A16=A7:C16=B7:B16=A7:C16=C7:C16=A7:C16=E7#+I7#+M7#=SUM(Q7:S7)
7112TRUETRUEFALSETRUETRUEFALSEFALSEFALSETRUE2215
8abbTRUEFALSEFALSEFALSETRUETRUEFALSETRUETRUE1225
9cdcTRUEFALSETRUEFALSETRUEFALSETRUEFALSETRUE2125
10222TRUETRUETRUETRUETRUETRUETRUETRUETRUE3339
11123TRUEFALSEFALSEFALSETRUEFALSEFALSEFALSETRUE1113
12213TRUEFALSEFALSEFALSETRUEFALSEFALSEFALSETRUE1113
13321TRUEFALSEFALSEFALSETRUEFALSEFALSEFALSETRUE1113
14abcTRUEFALSEFALSEFALSETRUEFALSEFALSEFALSETRUE1113
15ccbTRUETRUEFALSETRUETRUEFALSEFALSEFALSETRUE2215
16bacTRUEFALSEFALSEFALSETRUEFALSEFALSEFALSETRUE1113
17
18Note: There is also a solution to do iterations by rows, but I chose column orientation iterations for being the short dimension.
19
20T_P does sum of arrays clm i=a recursively,T_PN with REDUCE
21The rows that deliver 1,1,1 coresponds to the rows of "a" that hold unique values
22On other words, if we sum by rows, the rows with unique values =3 or =clms(a)
23T_P does the sum by rows with MMULT, T_PN with by BYROW
24
25=MMULT(Q7#,SEQUENCE(3)^0)=BYROW(Q7#,LAMBDA(x,SUM(x)))
2655
2755For final result both use FILTER
2855
2999=FILTER(A7:C16,J26#=3)=T_P(A7:C16)
3033123123
3133213213
3233321321
3333abcabc
3455bacbac
3533
36
ARRG np1
Cell Formulas
RangeFormula
E6,M29,Q29,E25,J25,Q6,U6,M6,I6E6=FORMULATEXT(E7)
E7:G16E7=A7:A16=A7:C16
I7:K16I7=B7:B16=A7:C16
M7:O16M7=C7:C16=A7:C16
Q7:S16Q7=E7#+I7#+M7#
U7:U16U7=SUM(Q7:S7)
E26:E35E26=MMULT(Q7#,SEQUENCE(3)^0)
J26:J35J26=BYROW(Q7#,LAMBDA(x,SUM(x)))
M30:O34M30=FILTER(A7:C16,J26#=3)
Q30:S34Q30=T_P(A7:C16)
Dynamic array formulas.
 
T_PN(a) Tool function calculating Permutations indexes without repetitions, using New lambda helper functions REDUCE, BYROW.
Excel Formula:
=LAMBDA(a,LET(c,COLUMNS(a),FILTER(a,BYROW(REDUCE(0,SEQUENCE(c),LAMBDA(v,i,v+(INDEX(a,,i)=a))),LAMBDA(x,SUM(x)=c)))))
LAMBDA 1.2.1.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXY
1T_PN(a) : alternative of T_P(a) (recursive) , using NEW lambda helper functions (REDUCE,BYROW) - filters rows w/o dups
2
3If a=T_PA(n,c) "prints" all PERMUTATIONA(n,c) (permutations w repetitions) , T_PN(T_PA(n,c))=T_PN(a) will "print" all PERMUT(n,c) (permutations w/o repetitions)
4
5n,4,c,3
6=PERMUTATIONA(4,3)step 1 (clm1=a)+(clm2=a)+(clm3=a)
764check=REDUCE(0,SEQUENCE(COLUMNS(A11#)),LAMBDA(v,i,v+(INDEX(A11#,,i)=A11#)))
864=ROWS(A11#)check
9step 2 sum BYROW(a')24=PERMUT(4,3)
10=T_PA(4,3)aa'=BYROW(E11#,LAMBDA(a,SUM(a)))24=ROWS(P14#)
111113339a''NEW!! T_PN(a)
121122215step 3 FILTER(a,a''=clms(a))step 4 single cellrecursive one T_P(a)
131132215=FILTER(A11#,I11#=COLUMNS(A11#))=T_PN(A11#)=T_P(A11#)
141142215123123123
151212125124124124
161221225132132132
171231113134134134
181241113142142142
191312125143143143
201321113213213213
211331225214214214
221341113231231231
231412125234234234
241421113241241241
251431113243243243
261441225312312312
272111225314314314
282122125321321321
292131113324324324
302141113341341341
312212215342342342
322223339412412412
332232215413413413
342242215421421421
352311113423423423
362322125431431431
372331225432432432
382341113
392411113
402422125
412431113
422441225
433111225
443121113
453132125
463141113
473211113
483221225
493232125
503241113
513312215
523322215
533333339
543342215
553411113
563421113
573432125
583441225
594111225
604121113
614131113
624142125
634211113
644221225
654231113
664242125
674311113
684321113
694331225
704342125
714412215
724422215
734432215
744443339
75
ARRG np2
Cell Formulas
RangeFormula
A6,T13,K13,P13,A10,I10A6=FORMULATEXT(A7)
A7A7=PERMUTATIONA(4,3)
E7E7=FORMULATEXT(E11)
A8A8=ROWS(A11#)
B8,Q9:Q10B8=FORMULATEXT(A8)
P9P9=PERMUT(4,3)
P10P10=ROWS(P14#)
A11:C74A11=T_PA(4,3)
E11:G74E11=REDUCE(0,SEQUENCE(COLUMNS(A11#)),LAMBDA(v,i,v+(INDEX(A11#,,i)=A11#)))
I11:I74I11=BYROW(E11#,LAMBDA(a,SUM(a)))
K14:M37K14=FILTER(A11#,I11#=COLUMNS(A11#))
P14:R37P14=T_PN(A11#)
T14:V37T14=T_P(A11#)
Dynamic array formulas.
 
T_PF(a) Tool function calculating Permutations indexes without repetitions, using other Functions AFUSBYROW, AHCLEAN
Excel Formula:
=LAMBDA(a,AHCLEAN(AFUSBYROW(a,,1)))
LAMBDA 1.2.1.xlsx
ABCDEFGHIJKLMNOPQ
1T_PF(a) , alternative of T_P(a) or T_PN(a) , using other Functions (AFUSBYROW,AHCLEAN) - filters rows w/o dups
2( f,u,s are the filter, unique, sort, arguments of AFUSBYROW )
3step 1step 2
4permutationa(4,3)f,omitted,u,1,s,omittedn,omittedstep 3other functions
5n,4,c,3(unique by each row)(filters only full rows)single cellAFUSBYROW
6=T_PA(4,3)=AFUSBYROW(A7#,,1)=AHCLEAN(E7#)=T_PF(A7#)AHCLEAN
71111123123
811212124124
911313132132
1011414134134
1112112142142
1212212143143
13123123213213
14124124214214
1513113231231
16132132234234
1713313241241
18134134243243
1914114312312
20142142314314
21143143321321
2214414324324
2321121341341
2421221342342
25213213412412
26214214413413
2722121421421
282222423423
2922323431431
3022424432432
31231231
3223223
3323323
34234234
35241241
3624224
37243243
3824424
3931131
40312312
4131331
42314314
43321321
4432232
4532332
46324324
4733131
4833232
493333
5033434
51341341
52342342
5334334
5434434
5541141
56412412
57413413
5841441
59421421
6042242
61423423
6242442
63431431
64432432
6543343
6643443
6744141
6844242
6944343
704444
71
ARRG np3
Cell Formulas
RangeFormula
A6,M6,E6,I6A6=FORMULATEXT(A7)
A7:C70A7=T_PA(4,3)
E7:G70E7=AFUSBYROW(A7#,,1)
I7:K30I7=AHCLEAN(E7#)
M7:O30M7=T_PF(A7#)
Dynamic array formulas.
 
LAMBDA 1.2.1.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZ
1Task: Filter only the rows of an array that hold unique values, general use, all 3 methods T_P(a) (recursive),T_PN(a) (New lambda helper fctions),T_PF(a) (calls other Functions)
2sample created with RANDARRAY(10,10,1,30,1)
3sample=AFUSBYROW(A4:J23,,1)
42522519202034928252192034928
5612231329197429861223132919748
6182718252826252622182725282622
71318151925214119231318151925214123
84111032871413162041110328714131620
915161832115171881215161832117812
1023231322717192522313227171925
1112462771917272641246277191726
12741788236162828741782361628
13212126821631139212682163119
14552320123813152320128131
15191142826829122010191142826829122010
162920177316121228182920177316122818
17514297281627243055142972816272430
188307253091920195830725919205
191926302782212231219263027822122312
202913539231027921291353923102721
21187382520941426187382520941426
222020292743141021222029274314102122
231682125211515191711168212515191711
24
25NEW lambda helper functions methodAFUSBYROW method, other Functions method
26=T_PN(A4:J23)=T_PF(A4:J23)
274111032871413162041110328714131620
28191142826829122010191142826829122010
291926302782212231219263027822122312
30187382520941426187382520941426
31
32recursive method
33=T_P(A4:J23)
3441110328714131620
35191142826829122010
3619263027822122312
37187382520941426
38
ARRG np4
Cell Formulas
RangeFormula
L3,A33,L26,A26L3=FORMULATEXT(L4)
L4:U23L4=AFUSBYROW(A4:J23,,1)
A27:J30A27=T_PN(A4:J23)
L27:U30L27=T_PF(A4:J23)
A34:J37A34=T_P(A4:J23)
Dynamic array formulas.
 
Recap: Combinations
COMBINA(n,c) returns the number of combinations with repetitions for a given number of items.
COMBIN(n,c) returns the number of combinations for a given number of items (w/o repetitions).
For permutations order is important, for combinations order is not important.
If order is not important, if we choose only one type of order (ascending) and we filter by this condition any other permutations, will get the combinations indexes array.
T_CA(a) is a recursion function we created to filter only the rows of an array "a" that are in ascending order.
If we apply this to the array of permutations w repetitions will get an array of combinations w repetitions.
If we apply same function, but this time, to the array of permutations w/o repetitions, we will get an array of combinations w/o repetitions.
T_PA(n,c) = a = array of permutations w repetitions.
T_P(a)=T_P(T_PA(n,c)) = array of permutations w/o repetitions.
T_CA(T_PA(n,c)) = array of combinations w repetitions.
T_CA(T_P(a))=T_CA(T_P(T_PA(n,c))) = array of combinations w/o repetitions.
Note: If we already have the array of combinations w repetitions, let's say "b", to get array of combinations w/o repetitions we can also apply T_P(b)
In other words, T_CA(T_P(a))=T_P(T_CA(a))
T_CAN(a) will replace the recursive one using New REDUCE
T_CAF(a) will replace the recursive one with other existing Functions AFUSBYROW
LAMBDA 1.2.1.xlsx
ABCDEFGHIJKLMNOPQRST
1Concept of T_CA(a), T_CAN(a)
2
3T_CA(a)=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))))
4
51. combinations with repetitions
6clm 1<=clm 2clm 2<=clm 3clm 3<=clm 4if we multiply all arraysfilter only rows in ascending order
7a=A8:A17<=B8:B17=B8:B17<=C8:C17=C8:C17<=D8:D17=F8#*H8#*J8#=FILTER(A8:D17,L8#)
81123TRUETRUETRUE11123
9abbaTRUETRUEFALSE02222
10cdceTRUEFALSETRUE01234
112222TRUETRUETRUE1abcd
121234TRUETRUETRUE1bccd
132132FALSETRUEFALSE0
143211FALSEFALSETRUE0=T_CA(A8:D17)
15abcdTRUETRUETRUE11123
16bccdTRUETRUETRUE12222
17bacdFALSETRUETRUE01234
18abcd
19For i=1 to clms(a)-1 we do clm i<=clm i+1 and we multiply the results.bccd
20(clm 1<=clm2)*(clm 2<=clm 3)*(clm 3<=clm 4)
21The rows that corespond to 1's values will be the rows in ascending order
22T_CA does this recursively, T_CAN will use REDUCE
23
242. "combinations" w/o repetitions
25Will have to apply same algoritm but this time , to an array that have no dups, OR, to apply T_P (keeps rows no dups) to array in T15
26
27=T_P(A8:D17)=T_CA(A28#)OR=T_P(O15#)
28123412341234
29abcdabcdabcd
30bacd
31
32directly from "a"
33a=T_CA(T_P(A34:D43))<=>=T_P(T_CA(A34:D43))
34112312341234
35abbaabcdabcd
36cdce
372222
381234
392132
403211
41abcd
42bccd
43bacd
44
ARRG np5
Cell Formulas
RangeFormula
F7,H7,J7,L7,K33,F33,K27,F27,A27,O14,O7F7=FORMULATEXT(F8)
F8:F17F8=A8:A17<=B8:B17
H8:H17H8=B8:B17<=C8:C17
J8:J17J8=C8:C17<=D8:D17
L8:L17L8=F8#*H8#*J8#
O8:R12O8=FILTER(A8:D17,L8#)
O15:R19O15=T_CA(A8:D17)
A28:D30A28=T_P(A8:D17)
F28:I29F28=T_CA(A28#)
K28:N29K28=T_P(O15#)
F34:I35F34=T_CA(T_P(A34:D43))
K34:N35K34=T_P(T_CA(A34:D43))
Dynamic array formulas.
 
T_CAN(a) Tool function calculating Combinations indexes, ( COMBINA ) using New lambda helper functions REDUCE.
If input array a=T_PA(n,c), T_CAN(a) returns combinations with repetitions indexes array of "n" by nr. chosen "c".
If input array a=T_P(T_PA(n,c)), T_CAN(a) returns combinations w/o repetitions indexes array of "n" by nr. chosen "c".
Excel Formula:
=LAMBDA(a,LET(c,COLUMNS(a),FILTER(a,REDUCE(1,SEQUENCE(c-1),LAMBDA(v,i,v*(INDEX(a,,i)<=INDEX(a,,i+1)))))))
LAMBDA 1.2.1.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAF
1T_CAN(a) , alternative of T_CA(a) (recursive) , using NEW lambda helper functions (REDUCE) - filters only rows in ascending order, order is not important
2
31. if a=T_PA(n,c) "prints" all PERMUTATIONA(n,c) (permutations w repetitions) , T_CAN(a)=T_CAN(T_PA(n,c)) will "print" all COMBINA(n,c) (combinations w repetitions)
42. if b=T_PN(a) "prints" all PERMUT(n,c) (permutations w/o repetitions) , T_CAN(b)=T_CAN(T_PN(a))=T_CAN(T_PN(T_PA(n,c))) will "print" all COMBIN(n,c) (combinations w/o repetitions)
5
61. combinations w repetitions
7permutationa(4,3)step 1 <=> for i=1 to clms(a)-1, 1*(clm i<=clm i+1)
8n,4,c,3=LET(a,A10#,c,COLUMNS(a),REDUCE(1,SEQUENCE(c-1),LAMBDA(v,i,v*(INDEX(a,,i)<=INDEX(a,,i+1)))))
9=T_PA(4,3)aa'check
101111=COMBINA(4,3)2. combinations w/o repetitionssame result with recursion
11112120b=T_PN(a)T_CAN(b)T_CA(b)
121131=ROWS(K17#)=T_PN(A10#)=T_CAN(S13#)=T_CA(S13#)
13114120123123123
141210step 2, FILTER(a,a')step 3 single cell124124124
151221NEW!! T_CAN(a)recursive one T_CA(a)132134134
161231=FILTER(A10#,E10#)=T_CAN(A10#)=T_CA(A10#)134234234
171241111111111142
181310112112112143check
191320113113113213=COMBIN(4,3)
2013311141141142144
211341122122122231=ROWS(W13#)
2214101231231232344
231420124124124241
241430133133133243or directly from "a"
251441134134134312=T_CAN(T_PN(A10#))=T_PN(T_CAN(A10#))
262110144144144314123123
272120222222222321124124
282130223223223324134134
292140224224224341234234
302210233233233342
312221234234234412
322231244244244413
332241333333333421
342310334334334423
352320344344344431
362331444444444432
372341
382410
392420
402430
412441
423110
433120
443130
453140
463210
473220
483230
493240
503310
513320
523331
533341
543410
553420
563430
573441
584110
594120
604130
614140
624210
634220
644230
654240
664310
674320
684330
694340
704410
714420
724430
734441
74
ARRG np6
Cell Formulas
RangeFormula
E8E8=FORMULATEXT(E10)
A9,W25,AA25,W21,W19,G16,K16,O16,W12,AA12,O12,S12,O10A9=FORMULATEXT(A10)
A10:C73A10=T_PA(4,3)
E10:E73E10=LET(a,A10#,c,COLUMNS(a),REDUCE(1,SEQUENCE(c-1),LAMBDA(v,i,v*(INDEX(a,,i)<=INDEX(a,,i+1)))))
O11O11=COMBINA(4,3)
O13O13=ROWS(K17#)
S13:U36S13=T_PN(A10#)
W13:Y16W13=T_CAN(S13#)
AA13:AC16AA13=T_CA(S13#)
G17:I36G17=FILTER(A10#,E10#)
K17:M36K17=T_CAN(A10#)
O17:Q36O17=T_CA(A10#)
W20W20=COMBIN(4,3)
W22W22=ROWS(W13#)
W26:Y29W26=T_CAN(T_PN(A10#))
AA26:AC29AA26=T_PN(T_CAN(A10#))
Dynamic array formulas.
 

Forum statistics

Threads
1,223,881
Messages
6,175,161
Members
452,615
Latest member
bogeys2birdies

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