RTUComputer ScienceYr 2023 · Sem 42023

Q2Theory of Computation

Question

4 marks

Design a DFA to accept binary numbers divisible by 3.

Answer

Track the running remainder mod 3 across three states, updating it as each new bit doubles the value and adds the incoming bit.

A binary string read left to right represents a number whose value can be tracked incrementally: if the value read so far is , and the next bit is , the new value is . Since we only care about divisibility by 3, we can track instead of itself, which takes only 3 possible values: 0, 1, 2. This lets us build a DFA with exactly 3 states, one per remainder class, where state means 'the binary string read so far represents a number .'

Define with , , start state (the empty string represents 0, which is divisible by 3), and (accept iff remainder is 0). The transition function models :

  • since
  • since
  • since
  • since
  • since
  • since

Consider the input string 1001, which represents the decimal number 9 (divisible by 3, so it must be accepted). Starting at : reading '1' goes to (remainder 1, matching value 1); reading '0' goes to (remainder 2, matching value 2); reading '0' goes to (remainder 1, matching value 4, and , correct); reading '1' goes to (remainder 0, matching value 9, and , correct). The machine halts in , so 1001 is accepted, matching the fact that 9 is divisible by 3. As a contrasting trace, the string 101 (decimal 5) goes , ending in the non-final state , correctly rejecting 5 since .

The correctness follows by induction on string length: the invariant 'after reading prefix , the DFA is in state where ' holds trivially for the empty prefix (, state ), and is preserved by each transition by construction, since each transition exactly implements . Hence for the full string , the DFA ends in with , and accepts exactly when , i.e., when the binary number is divisible by 3. This is a canonical example of the general technique of building a DFA to track a numeric invariant modulo a constant, which generalizes to divisibility by any using states.

Back to Paper