how would I build a generalized DFA for decimal Multiples of k?

237 views Asked by At

Lets say I have a language like this: L_k:={w∈Σ∗: (w)10=i·k, i∈N}. now using k as a parameter


I now want to define a DFA, that accepts L_{k}- and proof then that this DFA is valid. Important: k is a parameter, so the conditions and transition function is dependent on k.

I'd greatly appreciate any help!

2

There are 2 answers

0
narfi99 On

A simple solution is to store, in each state, the current and next remainder after division by k;

the next remainder is after the one you would have after reading a 0.

So you would use states S={(i,j) : 0≤i,j<k\}, where (0,0) is the initial state.

After reading a digit q, you would move from (i,j) to (j+q mod k, 10(j+q) mod k).

Every state (0,j) is final, as the current remainder is 0 there.

To prove that this is correct, let x denote some decimal number with the next remainder j. So x0 has remainder j. If we read a digit other than 0, say q, then we would have xq, which has remainder j+q mod k (follows immediately from modulo arithmetic, as xq-x0 = q), and the next remainder is the remainder of xq0 = xq * 10, so simply (j+q)*10 mod k.

So i is indeed the current remainder, and j the next remainder.

0
Patrick87 On

The way I am reading this is you want a DFA that accepts the language 0, 4, 8, 12, 16, ..., 4n, ..., for k = 4, or 7, 14, 21, 28, ..., 7n, ... for k = 7.

To pull this off, your DFA will always start in an initial state corresponding to a remainder after division by k of 0. This state is always accepting since 0 is an integer multiple of any k.

Now, when we read one digit, what we are doing is updating our understanding of the number we've read so far as follows: the number we've understood so far is actually ten times greater than we thought, plus this digit. Let's say we're in a state that corresponds to a remainder after division by k equal to m, and the next digit we read is d. Then:

m' = (10 * m + d) % k

We can get the remainder after division by k of the newly understood number by taking the remainder after division we've understood so far and transforming it. Because all we need to keep track of for this construction to work is the remainder after division by k of the number understood so far (we get d for free by reading input, and can forget it immediately), we only ever need exactly k states in our DFA.

How does this look for k = 7?

m = 0, d = 0: m' = 0; transition from q0 to q0 on symbol 0
       d = 1: m' = 1; transition from q0 to q1 on symbol 1
       d = 2: m' = 2; transition from q0 to q2 on symbol 2
       d = 3: m' = 3; transition from q0 to q3 on symbol 3
       d = 4: m' = 4; transition from q0 to q4 on symbol 4
       d = 5: m' = 5; transition from q0 to q5 on symbol 5
       d = 6: m' = 6; transition from q0 to q6 on symbol 6
       d = 7: m' = 0; transition from q0 to q0 on symbol 7
       d = 8: m' = 1; transition from q0 to q1 on symbol 8
       d = 9: m' = 2; transition from q0 to q2 on symbol 9
...
m = 6, d = 0: m' = 4; transition from q6 to q4 on symbol 0
       d = 1: m' = 5; transition from q6 to q5 on symbol 1
       d = 2: m' = 6; transition from q6 to q6 on symbol 2
       d = 3: m' = 0; transition from q6 to q0 on symbol 3
       d = 4: m' = 1; transition from q6 to q1 on symbol 4
       d = 5: m' = 2; transition from q6 to q2 on symbol 5
       d = 6: m' = 3; transition from q6 to q3 on symbol 6
       d = 7: m' = 4; transition from q6 to q4 on symbol 7
       d = 8: m' = 5; transition from q6 to q5 on symbol 8
       d = 9: m' = 6; transition from q6 to q6 on symbol 9

We get a DFA with 7 states and 70 transitions. In general, this method will always give a DFA with k states and 10k transitions.