Regular Expression to Determine if a Base 10 Number is Divisible by 5

This regular expression is a really easy one to write, but it will highlight that when writing regular expressions, it is sometimes easier to skip writing the DFA; which provided to be a useful step when I created a regular expression to determine if a base 10 number is divisible by 3.

The Problem:

Let L = { w | w mod 5 = 0 }, where the alphabet is {0,1,2,3,4,5,6,7,8,9}; give the DFA for L, and convert this in to a regular expression.

 

The Solution:

The DFA ( S, Σ, T, s, A ):
S = {q0,q1}
Σ = {0,1,2,3,4,5,6,7,8,9}
T = (doing the state diagram below)
s = {q0}
A = {q0}

For shorthand I will divide the alphbet, Σ, into:

  • A={0,5}
  • B={1,2,3,4,6,7,8,9}

The state diagram:

DFA State Diagram

Now to convert the DFA state diagram into a regular expression. This is done by converting the DFA into GNFA, and then converting the GNFA into a Regular Expression.

Conversion of DFA into GNFA, and removal of q0 state
Notice in the above that I did two steps in one; I first converted the DFA into a GNFA (which is the easy part), then I removed the q0 state.

Finally, removing the q1 state:

Removing the q1 state

Therefore the Regular Expression that defines the Regular Language L is:
A∪B(B∪A+B)*A+

For further reading please see "Introduction to the Theory of Computation" by Michael Sipser

© Erik Vold 2007-2010. Contact Erik Vold. Top ^