5
$\begingroup$

I would like to do something like CellularAutomaton[10, {1, 0, 0, 0, 0}, 2^5], but instead of running for 2^5 steps, it should stop as soon as a configuration has been seen twice, and report the number of such steps taken. Is it possible to do this?

$\endgroup$
5
$\begingroup$

You can use the one-step functional version with NestWhileList:

NestWhileList[CellularAutomaton[30, #] &, {1, 0, 0, 0, 0}, UnsameQ, All]

{{1, 0, 0, 0, 0}, {1, 1, 0, 0, 1}, {0, 0, 1, 1, 1}, {1, 1, 1, 0, 0}, {1, 0, 0, 1, 1}, {0, 1, 1, 1, 0}, {1, 1, 0, 0, 1}}

For the number of steps taken until a configuration repeats:

Length @ %

7

Note: In general, CellularAutomaton[rule, init, n] can also be obtained by nesting the one-step functional version using NestList[CellularAutomaton[rule, #]&, init, n]:

CellularAutomaton[30, {0, 0, 1, 0, 0}, 2^5] == 
  NestList[CellularAutomaton[30, #] &, {0, 0, 1, 0, 0}, 2^5]

True

$\endgroup$
  • 1
    $\begingroup$ One could use UnsameQ instead of DuplicateFreeQ @ {##} & in the third argument. $\endgroup$ – J. M. is away Oct 17 '18 at 1:20
  • $\begingroup$ @J.M. Right; much better. Thank you. $\endgroup$ – kglr Oct 17 '18 at 2:26
1
$\begingroup$
Clear[f]

f[state_] := f[f[state] = CellularAutomaton[30, state]]

{f[{1, 0, 0, 0, 0}], Length@DownValues[f]}

{{0, 0, 1, 1, 1}, 7}

$\endgroup$

Your Answer

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

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