# DFA for a language that accepts the addition of multiple of 2 and multiple of 5

I am trying to draw a DFA that accepts the following language:

$$L=\{a^{2i+5j} : i , j \geq 0\}$$

I started out by drawing an NFA, which I can then convert to a DFA but I am not sure if my NFA is correct. • Your language contains all words except $a,aaa$. May 18, 2019 at 21:15
• Add a transition from q5 to q0. The state q6 can be removed May 19, 2019 at 2:34

I'm not sure if this NFA is correct since both outgoing options from q0 are 'a', which means that you would not know to which node to go to. Wikipedia also defines an NFA as an FA when "each of its transitions is uniquely determined by its source state and input symbol", and less relevant "reading an input symbol is required for each state transition". The transistions of node q0 are not uniquely defined in this case.

Furthermore, even if two outgoing 'a's are allowed, once you have gone to q2, no multiples of 2 are possible anymore in your NFA.

That said, I do not know if you can really draw a good NFA since your definition of L allows for quite some correct multiples of a. You could start by making a list of all possible options: $$[0,2,4,6,8,10] \;for\; (i \geq 0 , j= 0)\\ [0,5,10,15,20] \;for\; (i=0,j\geq 0) \\ [7,9,11,13,15] \;for\; (i\geq1, j=1)\\$$ From which you can quickly deduce that after 4, every even and uneven numbers are possible. Which gives the following NFA: But I'm actually guessing you just wish to parse every multiple of 5 or 2, with tokens of either "aa" or "aaaaa". In which case you need the following NFA: This works, since a parser would restart after 5 'a' s since no other state can be reached. Furthermore, it can parse "aaaa", since it remembers the last accepting state, which would be q2, and starts over with the 'leftover' bit of "aa". Which it can parse again.

Normally you would also have to take into account the fact that a parser is greedy, however, this parser is rather simple, as such this is not a problem. However, in long strings of 'a' you would only get tokens of "aaaaa" until the end. At that point it might parse tokens of 2 in the case of leftover strings of "aa" or "aaaa".

SIDENOTE: Do not start your NFA with an accepting state of q0, since that means you could have infinitely many empty tokens.

• Forgive if I am being too direct. There have been multiple elementary errors in the answer. May 19, 2019 at 2:40
• What are infinitely many empty tokens? Where did you get those from?
– Evil
May 19, 2019 at 3:54
• @Apass.Jack can you please explain the errors? May 19, 2019 at 10:34
• @Evil What I meant is that when you implement this DFA, trying to parse only "a". You would go to an error state after q1, accept your last accepting state q0, which is empty (""). After which it will again try to parse leftover bit, namely "a", and continue like this forever. However, this is only true for implemented parsers by a computer programm, and I suppose I did assume this would be the case. May 19, 2019 at 10:39

All natural numbers are expressible in the form $$2x+5y$$, except for $$1,3$$. You can check this directly, of use the solutino of the Frobenius problem for two denominations. Therefore another way to write your language (assuming the alphabet is $$\{a\}$$) is $$\overline{\{a,a^3\}}.$$ You can construct a DFA for this language using 5 states, that count up to 4 and then saturate.

If you want to implement the constraint $$2x+5y$$ directly as an NFA (which is more economical for larger values of $$2,5$$), then you construct the following NFA:

• States: $$q_0,a_1,b_1,b_2,b_3,b_4$$.
• Initial state: $$q_0$$.
• Final states: $$\{q_0\}$$.
• Transition function: $$\begin{array}{c|cccccc} q & q_0 & a_1 & b_1 & b_2 & b_3 & b_4 \\\hline \delta(q,a) & a_1,b_1 & q_0 & b_2 & b_3 & b_4 & q_0 \end{array}$$

In words, there is a 2-cycle and a 5-cycle sharing a vertex $$q_0$$, which is also the initial state and the unique final state.

Yes, the NFA looks correct to me.

• $q0 -> q1 -> q0 -> q2 -> q3 -> q4 -> q5 -> q6$ , do you see anything wrong with this run? q6 is an accepting state. May 19, 2019 at 7:19
• Care to explain the reason of the downvote? May 19, 2019 at 7:20
• you're right, that does recognise $a^7$.
– rici
May 19, 2019 at 7:34