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
-
1$\begingroup$ One could use
UnsameQ
instead ofDuplicateFreeQ @ {##} &
in the third argument. $\endgroup$ – J. M.'s ennui♦ Oct 17 '18 at 1:20 -
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}
-
$\begingroup$ Interesting solution! This looks a bit like Floyd's algorithm. $\endgroup$ – J. M.'s ennui♦ Oct 19 '18 at 14:40