简体   繁体   中英

Design DFA accepting decimal strings divisible by 7

I'm a student studying DFAs looking for a DFA that could find if a decimal number is divisible by 7.

today I've solved divisibility problem for numbers 2,3,4,5,6,8,9 but I can't solve this problem for number 7. I've searched the web but I couldn't find any answer helping me or being understandable for me.

so now I'm here looking for help. thanks in advance.

The basic idea is that we will keep track of the current value, modulo seven, of the number we've seen so far. Each new digit takes the old number, multiplies by ten, and adds the new digit. Therefore, from the state corresponding to x (mod 7), adding digit d to the right means we go to the state corresponding to 10x + d (mod 7). This DFA has 70 states (the number of digits 0-9 times the number of remainders after division by seven 0-6).

q    s    q'
------------
q0   0    q0
q0   1    q1
q0   …    …
q0   6    q6

q1   0    q3
q1   1    q4
q1   …    …
q1   6    q2

…

q6   0    q4
q6   1    q5
q6   …    …
q6   6    q3

Consider the processing of the number 36736:

(q0) --3--> (q3) --6--> (q1) --7--> (q3) --3--> (q5) --6--> (q0)
0           0*10+3      3*10+6      1*10+7      3*10+3      5*10+6
            0+3         30+6        10+7        30+3        50+6
            3           36          17          33          56
            3           1           3           5           0

This number is divisible by seven because we end up in state q0, the state corresponding to zero modulo seven - meaning an even multiple of seven.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM