[Math] A Turing machine which computes $x^y$

automataturing-machines

Question:

Design a Turing machine which takes two integers $x$ and $y$ and outputs $x^y$.

Note 1: The representation of $x$ and $y$ is not mentioned in the question but you can use either the binary or the unary one.

Note 2: I know how to design a Turing machine for $x\cdot y$ and I think it can be helpful in designing the machine which the question wants. But I don't know how. Any idea?

Thanks in advance.

Best Answer

You say you know how to design a Turing machine for $x\cdot y$.

Do you know how to check if a number is $0$, and go to a halting state?

Can you subtract $1$ from a number?

Start off the input with x#y, where # is some separator symbol. Append $1$, giving x#y#1. Now encode the following:

  1. If the second number is $0$, halt (with potential cleanup for output first).
  2. Multiply the third number by the first number.
  3. Subtract $1$ from the second number.
  4. Goto 1.