I'd like to know how can I calculate function A mod B, where A > B and A, B are unary numbers, with a deterministic turing machine with a single tape.
Thanks
I'd like to know how can I calculate function A mod B, where A > B and A, B are unary numbers, with a deterministic turing machine with a single tape.
Thanks
Given an input like B111...10111...1BBB..., where the first string of 1s is a unary encoding of a (i.e., 1^a) and the second string of 1s is a unary encoding of b (i.e., 1^b), we can design a single-tape deterministic Turing machine to compute a mod b (by entering halt-accept after leaving the unary representation of a mod b left on the tape).
Note first that a mod b < a, so we can recover the unary representation of a mod b by erasing some of the 1s from a's unary representation, and all of the 1s from b's unary representation. Observe that a mod b = (a - b) mod b, at least when a >= b; when a < b, then a mod b = a. This observation suggests that we can erase b 1s from a's unary representation until we have fewer than b 1s remaining, at which point we erase the 1s from b's representation and halt-accept.
Pseudocode:
move right until you find a blank.
move one step to the left.
you are now looking at the last 1 in b's representation.
mark this as Y and move left until you find 0.
move left until you find a 1 or blank.
you are now looking at the last 1 in a's representation, or blank.
if 1, mark this as X and move right until you find Y.
if blank, a < b; change all Xs to 1s and all 0s, 1s and YBs to blanks. halt-accept.
move one step to the left.
you are now looking at the last 1 in b's representation, or 0.
if 1, continue as above.
if 0, b < a; change all Xs to 0s, all Ys to 1s, and restart from the beginning
Example: 10 mod 3
B11111111110111BBB...
^
B11111111110111BBB...
^ move right until you find a blank
B11111111110111BBB...
^ move one step to the left. looking at last 1 in b
B1111111111011YBBB...
^ mark as Y and move left to 0
B1111111111011YBBB...
^ move one step to the left. looking at last 1 in a.
B111111111X011YBBB...
^ mark as X and move right to Y
B111111111X011YBBB...
^ move one step to the left. looking at last 1 in b.
B111111111X01YYBBB...
^ mark as Y and move left to 0
B111111111X01YYBBB...
^ move left to 1
B11111111XX01YYBBB...
^ mark as X and move right to Y
B11111111XX01YYBBB...
^ move one step to the left. looking at last 1 in b
B11111111XX0YYYBBB...
^ mark as Y and move left to 0
B11111111XX0YYYBBB...
^ move left to 1
B1111111XXX0YYYBBB... mark as X and move right to Y
^
B1111111XXX0YYYBBB... move one step left. looking at 0; b < a
^
B11111110000111BBB...
^ change Xs to 0s and Ys to 1s; start over.
(above process repeats two more times)
B10000000000111BBB...
^ erased 3x 1s from a 3x times
B10000000000111BBB...
^ move right to blank
B10000000000111BBB...
^ move one step left. looking at last 1 in b
B1000000000011YBBB...
^ mark as Y and move left to 0.
B1000000000011YBBB...
^ move left to 1
BX000000000011YBBB...
^ mark as X and move right to Y
BX000000000011YBBB...
^ move one step left. looking at last 1 in b.
BX00000000001YYBBB...
^ mark as Y and move left to 0
BX00000000001YYBBB...
^ move left to blank. a < b.
B1BBBBBBBBBBBBBBBB...
^ change Xs to 1s and 0s, 1s, Ys to blank. halt-accept