Ternary representation of an integer that is a multiple of 5

Consider the problem of building a Deterministic Finite Automaton (DFA) over \{0, 1, 2\} to accept ternary numbers that are multiples of 5. For the sake of simplicity, assume that the empty string, \epsilon, should be accepted.

States

The resulting DFA will have 5 states. Each state will keep track of one of five possible remainders – 0, 1, 2, 3, 4.

Rendered by QuickLaTeX.com

For example, the state r_3, keeps track of the information that the ternary string read upto this point, when divided by 5, leaves remainder 3.

Tracking remainder

Consider the ternary string 122. The decimal equivalent of this ternary number is 17. 17 when divided by 5, leaves remainder 2. Thus, if the above DFA already read the input 122, it will currently be in state r_2.

Symbol 0

If the string 122 is followed by a 0, the ternary number 1220 is equivalent to 51 in decimal. In effect, seeing a single 0 at the end of a ternary string, multiplies the integer equivalent by 3. The same logic applies to the remainder. 51 when divided by 5, leaves remainder 1, which is the same as (2 * 3) mod 5 \equiv 1.

Old TernaryOld Decimalmod 5SymbolNew TernaryNew Decimalmod 5
1221720122017 * 3 = 51(2*3) mod 5 \equiv 1

Symbol 1

If the string 122 is followed by a 1, the ternary number 1221 is equivalent to 52 in decimal. In effect, seeing a single 1 at the end of a ternary string, multiplies the integer equivalent by 3 and then adds a 1. The same logic applies to the remainder. 52 when divided by 5, leaves remainder 2, which is the same as (2 * 3 + 1) mod 5 \equiv 2.

Old TernaryOld Decimalmod 5SymbolNew TernaryNew Decimalmod 5
1221721122117 * 3 + 1 = 52(2*3 + 1) mod 5 \equiv 2

Symbol 2

If the string 122 is followed by a 2, the ternary number 1222 is equivalent to 53 in decimal. In effect, seeing a single 2 at the end of a ternary string, multiplies the integer equivalent by 3 and then adds a 2. The same logic applies to the remainder. 53 when divided by 5, leaves remainder 3, which is the same as (2 * 3 + 2) mod 5 \equiv 3.

Old TernaryOld Decimalmod 5SymbolNew TernaryNew Decimalmod 5
1221722122217 * 3 + 2 = 53(2*3 + 2) mod 5 \equiv 3

General Formula

Consider any ternary string w, that leaves remainder r when divided by 5. Upon seeing a new symbol after w, the remainder changes as follows

Ternary StringRemainderNew SymbolNew Remainder
wr0(3r) mod 5
wr1(3r + 1) mod 5
wr2(3r + 2) mod 5

r can take values from 0 to 4. Substituting these values in the above table, generates the following

RemainderNew SymbolNew Remainder
00(3 * 0) mod 5 \equiv 0
01(3 * 0 + 1) mod 5 \equiv 1
02(3 * 0 + 2) mod 5 \equiv 2
10(3 * 1) mod 5 \equiv 3
11(3 * 1 + 1) mod 5 \equiv 4
12(3 * 1 + 2) mod 5 \equiv 0
20(3 * 2) mod 5 \equiv 1
21(3 * 2 + 1) mod 5 \equiv 2
22(3 * 2 + 2) mod 5 \equiv 3
30(3 * 3) mod 5 \equiv 4
31(3 * 3 + 1) mod 5 \equiv 0
32(3 * 3 + 2) mod 5 \equiv 1
40(3 * 4) mod 5 \equiv 2
41(3 * 4 + 1) mod 5 \equiv 3
42(3 * 4 + 2) mod 5 \equiv 4

Transitions

In the above table, the first column maps to one of the states of the DFA. The second column, maps to a transition symbol. The third column maps to the state the transition points to. Converting the table to states and transitions results in

From StateSymbolTo State
r_00r_0
r_01r_1
r_02r_2
r_10r_3
r_11r_4
r_12r_0
r_20r_1
r_21r_2
r_22r_3
r_30r_ 4
r_31r_0
r_32r_1
r_40r_2
r_41r_3
r_42r_4

Since, the DFA has to accept strings that are multiples of 5, the state r_0 will be the only final state.

DFA

Combining the states and the transition table, the resulting DFA is shown below

Rendered by QuickLaTeX.com

Leave a Reply

Your email address will not be published. Required fields are marked *