I would like to generate tuples of length 21 with possible elements 1, 2, and 3. I would also like there to be an equal number of 1's, 2's, and 3's, and I want to control for how many times one element is equal to the element before it in each tuple. That's a bit hard to word, so here is what I've done.
I have my criteria:
sieve[combination_] := Count[combination, 1] === Count[combination, 2] === Count[combination, 3] && Count[Table[combination[[i]] == combination[[i + 1]], {i, 1, 20}], False] === 5;
Then I generate the tuples:
data = Select[Tuples[{1, 2, 3}, {21}], sieve]
This works readily for length 9, but for 21 it exhausts my memory. Is there a way I can do this computation?
sieve
function seems to be hard-wired for the 21-tuple case so it fails for e.g. n=9. Can you make it more general so we can play with ideas? Also, take a look atSelectTuples
in the Function Repository. $\endgroup$