6
$\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$
0

2 Answers 2

6
$\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$
2
  • 1
    $\begingroup$ One could use UnsameQ instead of DuplicateFreeQ @ {##} & in the third argument. $\endgroup$ Oct 17, 2018 at 1:20
  • $\begingroup$ @J.M. Right; much better. Thank you. $\endgroup$
    – kglr
    Oct 17, 2018 at 2:26
2
$\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$
1

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.