Exercise 1.19

Qi Ji

25th August 2019

Make a regular expression which contains all those decimal natural numbers which start with \(3\) or \(8\) and have an even number of digits and end with \(5\) or \(7\).

\[\left\{3,8\right\}\cdot \left(\Sigma\cdot\Sigma\right)^* \cdot\left\{5,7\right\}\] where \(\Sigma = \left\{0,\dots,9\right\}\).

Make a further regular expression which contains all odd ternary numbers without leading \(0\)s.

First observe that a ternary number is odd iff the sum of all its digits is odd. Define \[\begin{align*} \texttt{TWO} &= \left\{2\right\}\cdot\left\{0,2\right\}^* \\ \texttt{ODD} &= \texttt{TWO}^*\cdot \left\{1\right\} \\ \texttt{EVEN} &= \left\{0,2\right\}\cup\left(\left\{1\right\}\cdot\left\{0,2\right\}^*\cdot\left\{1\right\}\right) \end{align*}\] and we match all ternary numbers with the expression \[ \texttt{ODD}\cdot\texttt{EVEN}^*. \]