5.1. The sequence is defined by the relations $a_{1}=1$,

$$
a_{2 n}=\left\{\begin{array}{ll}
a_{n}, & \text { if } n \text { is even, } \\
2 a_{n}, & \text { if } n \text { is odd; }
\end{array} \quad a_{2 n+1}= \begin{cases}2 a_{n}+1, & \text { if } n \text { is even, } \\
a_{n}, & \text { if } n \text { is odd. }\end{cases}\right.
$$

Find the smallest natural $n$ for which $a_{n}=a_{2017}$.
Answer: 5.

Solution: The given rules can be easily interpreted in terms of the binary system: if $n$ ends in 0 and 1 is appended to the right, then 1 is appended to the right of $a_{n}$. If $n$ ends in 1 and 0 is appended, then 0 is appended to the right of $a_{n}$. In all other cases, $a_{n}$ does not change (when 0 is appended to 0 or 1 is appended to 1). Let's write the number 2017 in binary: $2017=11111100001_{2}$. It is easy to see that $a_{2017}=101_{2}=5_{10}$. By checking the first few values, we find $a_{5}=5$.