Forum Discussion
Efficient approach to generate list of combinations with no repetition
- 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) )
The formula I timed was the BYROW version, without any substitution by text or symbols. The MMULT version is slightly faster at 5000 ms. for the ²⁰C₁₀ case. The combinations can't go beyond n=20 because the intermediate calculation exceeds the available row count. Broadcasting text puts the timings up a shade once more.
=LET(
h, 2 ^ SEQUENCE(1, n, 0),
k, SEQUENCE(2 ^ n),
p, SIGN(BITAND(h, k)),
e, SEQUENCE(n, 1, 1, 0),
f, MMULT(p, e) = m,
FILTER(p, f)
)- davidlealApr 05, 2023Iron Contributor
PeterBartholomew1 Thanks, that is why I initially liked the recursive approach SETA, but due to REDUCE/VSTACK it is not efficient, because it generates exactly the total number of combinations, there is no need to generate more rows and then to filter, which allows going to n>=20, but because of its performance, it is not convenient. I hope Microsoft in the future improves REDUCE/VSTACK performance, it is a very useful pattern, but it is time-consuming. I added a https://feedbackportal.microsoft.com/feedback/idea/d90548de-bdd3-ed11-a81b-000d3a7bb563 in case you would like to vote it up.