Forum Discussion
davidleal
Apr 04, 2023Iron Contributor
Efficient approach to generate list of combinations with no repetition
I am trying to find the best way to generate the combination with no repetition, i.e. the corresponding set related to the total of a combination of COMBIN(n,m) output. I found several approaches wit...
- Apr 04, 2023
Forgive me for asking, but have I got the wrong end of the stick here?
= IF(Combinationsλ(n,m), "●","-") Combinationsλ = LET( k, 2 ^ SEQUENCE(1, n, 0), h, SEQUENCE(2 ^ n), p, SIGN(BITAND(h, k)), f, BYROW(p, LAMBDA(p, SUM(p))) = m, FILTER(p, f) )
JosWoolley
Iron Contributor
Replacing your potentially costly DROP/REDUCE/VSTACK set-up in COMBIN.SETB with a simple wrap/unwrap combination might be worth a go:
=LAMBDA(x, m,
LET(
y, TOROW(x),
n, COLUMNS(y),
z, MOD(INT((SEQUENCE(2 ^ n) - 1) / 2 ^ SEQUENCE(, n, 0)), 2),
f, FILTER(z, MMULT(z, SEQUENCE(n) ^ 0) = m),
WRAPROWS(TOCOL(IF(IF(f, SEQUENCE(, n)), y, NA()), 2), m)
)
)
Regards
davidleal
Apr 04, 2023Iron Contributor
JosWoolley I tested with n=19, m=10, 92K rows to be generated, my version SETE, 2.9secs, your solution 3.20secs. I run several times and your solution was always bigger than 3secs, and mine lower than 3secs. Just a minor difference. WRAPROWS makes really the difference!!!