# Turing Machine to accept maximum of two numbers

Prerequisite – Turing Machine
Problem :
Draw Turing machine to accept maximum of two unary numbers n1 and n2 given as B0n1C0n2B. If two numbers are equal, then accept either oftwo numbers.

Logic Used :

1. For every 0 on left side, replace it with X and search for another 0 on right side. If found, replace it with X and move searching for 0 on left side.
2. If not found, it means that left side has more number of 0s that right side. That is n1 > n2. Hence change all Xs to Os and stop on leftmost side.
3. Keep doing step 1. If it happens that you point to C, after rightmost conversion (Replacing value of 0 with X on right side), then it implies that n2 >= n1. Hence convert all Xs to Os on right-hand side and stop.

Steps to be followed :

1. From initial state q0, on receiving 0, replace with X and move rightwards and change state q1. On receiving C, move rightwards and change state to q7.
2. From state q1, on receiving 0 keep moving right. On receiving C move rightwards and change state to q2.
3. From state q2 on receiving X remains in same state and keep moving right. On receiving 0, replace it with X, move leftwards and change state to q5. On receiving blank(B) move left and change state to q3.
4. From state q3, keep moving left on receiving 0 or C and on receiving X, replace X with 0 and move leftwards. Finally, on receiving blank space move right and change state to q4, accepting state for n1>n2.
5. From state q5, on receiving C move leftwards and change state to q6. But on receiving X or 0 stay in the same state and keep moving left.
6. From state q6, on receiving 0 keep moving left without changing state. But on receiving X replace it with 0 and move rightwards and change state back to q0.
7. From state q7, keep moving rightwards on receiving 0 and change every X with 0 while moving rightwards. On receiving B move leftward and change state to q8.
8. On receiving 0 in state q8, keep moving left and on receiving C move rightwards and change state to q9, accepting state for n2 >= n1.

Here, q0 shows initial state and q1, q2, ….., q9 are transition states and q4, q9 shows final state.
And 0, 1 is data inside machine and X, Y, C are variables used for finding maximum, i.e, comparing data and R, L shows right and left.

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

## Recommended Posts:

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.