Learning Scientific Programming with Python (2nd edition)

P4.3.3: Rule 30

Question P4.3.3

In A New Kind of Science, Stephen Wolfram describes a set of simple one-dimensional cellular automata in which each cell can take one of two values: 'on' or 'off'. A row of cells is initialised in some state (for example, with a single 'on' cell somewhere in the row) and it evolves into a new state according to a rule which determines the subsequent state of a cell ('on' or 'off') from its value and that of its two nearest neighbours. There are $2^3 = 8$ different states for these three 'parent' cells taken together and so $2^8=256$ different automata rules: that is, the state of cell $i$ in the next generation is determined by the states of cells $i-1$, $i$ and $i+1$ in the present generation.

Rule 30

These rules are numbered 0 – 255 according to the binary number indicated by the eight different outcomes each one specifies for the eight possible parent states. For example, rule 30 produces the outcome (off, off, off, on, on, on, on, off) (or 00011110) from the parent states given in the order shown in the figure below. The evolution of the cells can be illustrated by printing the row corresponding to each generation under its parent as shown in the figure.

Write a program to display the first few rows generated by rule 30 on the command line, starting from a single 'on' cell in the centre of a row 80 cells wide. Use an asterisk to indicate an 'on' cell and a space to represent an 'off' cell.