I can produce a variable size list of strings like AACBBAABC with this code:

test[n_] := 
  Flatten[ Table[ StringJoin @@ Table[ Subscript[\[Zeta], i], {i, 1, n}], ##] & @@ 
  Table[{ Subscript[\[Zeta], i], {"A", "B", "C"}}, {i, 1, n}], n - 1]

My problem is that ultimately n will be such that the table has $10^{17}$ elements.
What I want to do is generating the elements one at a time so that I can run a test on the string to validate it. The number of good strings will be $<<10^{17}$. In essence I need a code that will have a variable number of loops depending on n. My $70$ year old brain is having a problem coming up with an answer!

  • 4
    $\begingroup$ $10^{17}$ elements??? At 1 byte per element, that's about 100 peta bytes! Do you work for the NSA or Google? :D $\endgroup$ – rm -rf Nov 27 '13 at 20:39
  • 1
    $\begingroup$ If you could check a billion elements per second (a rate of 1 GHz), it would take approximately π years. You might want to figure out how to skip large chunks of elements. $\endgroup$ – Michael E2 Nov 27 '13 at 21:01
  • $\begingroup$ Your test function looks too complex. Why don't you use something simple and fast like test[n_] := StringJoin @@@ Tuples[{"A", "B", "C"}, {n}] $\endgroup$ – halirutan Nov 27 '13 at 21:11
  • $\begingroup$ test[n] equivalent to Tuples[{"A","B","C"},n]. What is it that you want to know about all tuples of this triplet? Perhaps you want to know how many there are? If so, this can be much more easily found that listing them all. BTW, what constitutes a good string? $\endgroup$ – DavidC Nov 27 '13 at 21:14
  • $\begingroup$ For a fixed length string, what you want is a Gray code. Alternatively, if you were interested in permutations, there's NextPermutation, and something similar could be created for Tuples. $\endgroup$ – rcollyer Nov 27 '13 at 21:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.