Vous êtes sur la page 1sur 4

Theory of Computation assignment

Construct a Turing machine which computes the multiplication of two unary numbers by giving
the complete formal definition and transition functions.
Due date: 03/10/16

Using the Extensions: Multiplication on the Turing Machine


Now, to show why it was worth all the trouble of figuring out how to add all of those extensions,
we'll describe how to build a Turing machine that does multiplication. Basically, it takes two
numbers in unary on the tape, like "111*1111=", and runs until it's written the product after the
"=": "111*1111=111111111111".
So here's what the machine does:
1. Put "start" and "end" marks at the beginning and end of the second number on the tape.
2. Put a "counter" mark at the start of the first number on the tape.
3. Put a "target" mark at the "=" on the tape.
4. Scan back to the counter mark. If it's on "*", halt. If not, move it forward by one cell.
5. Do a copy from the start and end marks to the target mark.
6. Put the target mark on the cell after the last digit that you copied.
7. Scan back to the "start" mark, and replace it with the end mark.
8. Scan back to the character after the "*", and write the start mark.
9. Go back to step 4.
Just to give you an idea of how an algorithmic description of the machine these extensions makes
things easier to read, here's the state transition table for a turing machine, which includes some of
our extensions, for essentially this multiplication algorithm (I found this program here after I
wrote the algorithm above, so it's not an exact match.)
#
#
#
#
#
#
#

Turing machine to multiply two numbers:


Number n is represented by string of n+1 1's.
Initially, tape is assumed to be in form of _n1_n2_ (where _ is blank),
and head is assumed to be positioned at leftmost non-blank square.
Initial internal machine state is "start".
At halt, tape is _n1_n2_n3_, where n3=n1*n2,
and head is positioned at leftmost non-blank square.

State,Symbol
start, 1

State,SymbolToWrite,
Direction
move1right, W, R

comment
# mark first bit of 1st argument

move1right, 1 move1right, 1, R

# move right til past 1st argument

move1right, _ mark2start, _, R

# square between 1st and 2nd arguments found

mark2start, 1 move2right, Y, R

# mark first bit of 2nd argument

move2right, 1 move2right, 1, R

# move right til past 2nd argument

move2right, _ initialize, _, R

# square between 2nd argument and answer


found

initialize, _

backup, 1, L

# put a 1 at start of answer

backup, _

backup, _, L

# move back to leftmost unused bit of 1st arg

backup, 1

backup, 1, L

# ditto

backup, Z

backup, Z, L

# ditto

backup, Y

backup, Y, L

# ditto

backup, X

nextpass, X, R

# in position to start next pass

backup, W

nextpass, W, R

# ditto

nextpass, _

finishup, _, R

# if square is blank, we're done. finish up

nextpass, 1

findarg2, X, R

# if square is not blank, go to work. mark bit

findarg2, 1

findarg2, 1, R

# move past 1st argument

findarg2, _

findarg2, _, R

# square between 1st and 2nd arguments

findarg2, Y

testarg2, Y, R

# start of 2nd arg. skip this bit, copy rest

testarg2, _

cleanup2, _, L

# if blank, we are done with this pass

testarg2, 1

findans, Z, R

# if not, increment ans. mark bit, move there

findans, 1

findans, 1, R

# still in 2nd argument

findans, _

atans, _, R

# square between 2nd argument and answer

atans, 1

atans, 1, R

# move through answer

atans, _

backarg2, 1, L

# at end of answer--write a 1 here, go back

backarg2, 1

backarg2, 1, L

# move left to first unused bit of 2nd arg

backarg2, _

backarg2, _, L

# ditto

backarg2, Z

testarg2, Z, R

# just past it. move right, and test it

backarg2, Y

testarg2, Y, R

# ditto

cleanup2, 1

cleanup2, 1, L

# move back through answer

cleanup2, _

cleanup2, _, L

# square between 2nd arg and answer

cleanup2, Z

cleanup2, 1, L

# restore bits of 2nd argument

cleanup2, Y

backup, Y, L

# done with that. backup to start next pass

finishup, Y

finishup, 1, L

# restore first bit of 2nd argument

finishup, _

finishup, _, L

# 2nd argument restored, move back to 1st

finishup, X

finishup, 1, L

# restore bits of 1st argument

finishup, W

almostdone, 1, L

# restore first bit of 1st arg. almost done

almostdone, _ halt, _, R

# done with work. position properly and halt

Vous aimerez peut-être aussi