# How to run a CellularAutomaton until a configuration is repeated?

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?

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

• One could use UnsameQ instead of DuplicateFreeQ @ {##} & in the third argument. – J. M. will be back soon Oct 17 '18 at 1:20
• @J.M. Right; much better. Thank you. – kglr Oct 17 '18 at 2:26
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}