2
$\begingroup$

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?

$\endgroup$
3
  • 4
    $\begingroup$ Perhaps you are looking for Shifrin's lazy tuples. $\endgroup$
    – Alan
    Commented Jun 22, 2020 at 21:34
  • 1
    $\begingroup$ Your 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 at SelectTuples in the Function Repository. $\endgroup$
    – MarcoB
    Commented Jun 22, 2020 at 21:46
  • $\begingroup$ Do you want a random selection of tuples or all tuples? If you wanted all of length 21 with 7 1's, 2's, and 3's, then there are 21!/7!^3= 399,072,960 arrangements. $\endgroup$
    – JimB
    Commented Jun 22, 2020 at 21:48

2 Answers 2

4
$\begingroup$
Clear["Global`*"]

pool = Table[Range[3], 7] // Flatten;

Rather than produce all tuples, produce a tuple on demand

choice := Module[{ch = RandomSample[pool]},
  While[Count[Most[ch] - Rest[ch], 0] != 15,
   ch = RandomSample[pool]]; ch]

choice

(* {2, 2, 3, 3, 3, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 3, 3, 3, 3, 2, 2} *)

For multiple tuples

Table[choice, 5] // Column

enter image description here

$\endgroup$
2
  • $\begingroup$ +1 For a length of 21, this clearly makes more sense. $\endgroup$
    – JimB
    Commented Jun 22, 2020 at 22:06
  • $\begingroup$ Brilliant, makes a lot more sense to do it this way. Thanks much! $\endgroup$
    – kangaroo
    Commented Jun 22, 2020 at 23:30
1
$\begingroup$

For a length of 18 (6 groups of 1, 2, and 3) one finds 17,153,136 permutations.

n = 6;
data = Flatten[Table[{1, 2, 3}, {n}]];
p = Permutations[data];
Length[p]
(* 17153136 *)

In general for a length of 3 n there will be (3 n)!/(n!^3) permutations. For 3 n = 21 there will be 399,072,960 permuations.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.