Solution (source code)

= Solution

The <period of a state in a Markov chain> $a$ is
$$
\boxed{d(a)=\gcd\{n\geq1:P^n(a,a)>0\}}.
$$
A state is aperiodic when this greatest common divisor is one.