This repository simulates a code for Turing Machine which perform multiplication operation followed by addition of one. f(x) = 7x + 1
For the given problem π (π₯) = 7π₯ + 1 πππ ππ£πππ¦ π₯ β₯ 0, it is required to essentially create a Turing Machine for multiplying two number such that π (π₯, y) = XY , finally at the end of the multiplication , an addition operation shall be performed to the output such that π (π₯,y) = π₯ + y.
In order to perform multiplication of two numbers, initially the numbers will be converted into unary system and then inserted into the tape. In the given problem, the tape alphabet will consist of {#, 0, 1, *, X, Y}.
Initially β#β will be inserted into the tape indicating start of an input, followed by 7 ones, followed by β*β sign.
On the right hand of the tape (after the β*β symbol) the multiplier will be inserted followed by β#β indicating end of input, assuming the multiplier is 2, then the tape will look like this:
The machine which performs the multiplication procedure followed by the addition is shown below:
-
In q0, if the read symbol is β#β, head to the right. If β1β was read, then change it to βXβ and move right, change state to q1.
-
In q1, if the read symbol is β1β keep moving right (basically escaping all the ones) until the β*β is reached, head to state q2 and move right.
-
In state q2 (now the reading hand is standing after the β*β symbol), if the read symbol is β1β, change it to βYβ and head toward state q3, and move right.
-
In q3, for every read β1β move right (basically escaping all the ones on the RHS) until β#β is reached, change state to q4 and move right.
-
In q4, if read input is βblankβ then insert β0β , move right, and head to state q5.
-
In q5, on reading either {0, #, 1, Y} keep heading to the left side of the tape until the multiplication symbol is reached, head to q2 and repeat the procedure of replacing every β1β with βYβ and adding β0βto the end of tape, until all the ones on the right side after the β*β changed to βYβ.
-
A rest procedure shall be applied to transform the βYβ into β1β again as long as there still β1βs on the LHS of the multiplication tape. This is performed during the transitions from state q6 to q8.
-
Eventually, when all the β1βs on the LHS of the multiplication symbol have turned into βXβ, this indicates that the multiplication procedure is completed and it is time to sum an extra 1 to the output of the multiplication left on the most RHS of tape, this is done by adding an extra β1β to the tape.
The complete version of the Turing Machine which simulates π (π₯) = 7π₯ + 1 πππ ππ£πππ¦ π₯ β₯ 0
The first part of the machine (RHS) is basically inserting the 7 ones followed by #. Then the machine explained above takes the wheel to compute the multiplication and addition operations.
Complete run output for the function in Java program: https://gist.github.com/MyCloud012/ccfdf8e6e99a50c8834b
If you are interested in this kind of science, I recommend textbook "Introcuction to Theory of computation by Micheal Sipser Ist Ed".
PS: The implementation is not following any of the principles for code modularity, its more like a handy implementation for simulation purposes.