如何创建一个图灵机作为 x^y 的函数计算器

发布于 2024-07-25 09:29:20 字数 494 浏览 10 评论 0原文

我正在学习图灵机测试,我遇到了一个问题,我必须创建一个图灵机作为函数计算器:

f(x,y) = x ^ y 

我知道我的磁带输入会像这样分开:

1's of base 0 1's of exponent 

用我的磁带输出就像

1's of base 0 1's of exponent 0 1's of the computed result

我如何将 X 和 Y 放在磁带上? (可以是多胶带机) 状态图会是什么样子?

注意:我使用一元,1 用于表示 0,0 不用作值而是用作分隔符。

所以:

   0 = delimiter
   1 =    0
   11 =   1
   111 =  2
   1111=  3
   11111= 4 
   etc.

I'm studying for a test on Turing machines, and I'm stuck with a problem in which I have to create a Turing Machine that serves as a function calculator for:

f(x,y) = x ^ y 

I understand my tape input would come separated like this:

1's of base 0 1's of exponent 

With my tape output being like

1's of base 0 1's of exponent 0 1's of the computed result

How would I go about placing X's and Y's on the tape(s)? (It can be a multi-tape machine)
What would the state diagram look like?

Note: I'm using unary with 1 used to represent 0 and 0 used not as value but as a delimiter.

So:

   0 = delimiter
   1 =    0
   11 =   1
   111 =  2
   1111=  3
   11111= 4 
   etc.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绳情 2024-08-01 09:29:20

我在这里猜测了一下,自从我玩图灵机模拟器以来已经有一段时间了。 首先,我想将任务分为几个概念步骤:

  1. 将磁带上的数字增加到磁带上另一个数字的值
  2. ,将磁带上的数字乘以磁带上另一个数字的值 - 这可以通过重复执行步骤 1 来完成
  3. 磁带上的一个数字的平方 - x ^ 2 的计算方法是将 x 放在磁带上,然后将其与其自身相乘
  4. (最后一步)将磁带上的一个数字乘以另一个数字的值磁带上的数字 - 这可以通过重复执行步骤 2 来完成。

要重复执行某项任务 N 次,请将 N 放入磁带上,执行该任务一次,然后从数字 N 的末尾减 1。重复,直到数字 N 为从磁带上消失了。

无论如何,希望这足以让您开始。 状态机可以或多或少以这种方式机械地构造。

I'm guessing a bit here, it's been a little while since I played with Turing machine simulators. First of all, I would want to divide the task up into several conceptual steps:

  1. increment a number on the tape by the value of another number on the tape
  2. multiply a number on the tape by the value of another number on the tape - this can be done by performing step 1 repeatedly
  3. square a number on the tape - x ^ 2 is worked out by putting x on the tape then multiplying it by itself
  4. (the final step) raise a number on the tape to the power of the value of another number on the tape - this can be done by performing step 2 repeatedly

To perform a task repeatedly N times, put N onto the tape, perform the task once, then subtract 1 from the end of the number N. Repeat until the number N is gone from the tape.

Hopefully this is sufficient to get you started anyway. The state machine can be constructed more or less mechanically in this fashion.

盗心人 2024-08-01 09:29:20

在我自己的图灵伪代码中:

  1. 将输入 A0B 复制到磁带 2
  2. 将 000010000 写入磁带 3
    • 将磁带 3 上的数字乘以磁带 2 中的 A
      1. 从A开头开始
      2. 将 0 写入磁带 4
        • 复制数字 3 => 4
        • 在磁带 3 上前进一次 (3++)
        • 除非 A 结束,否则转至步骤 3
        • 将答案从磁带 4 移至磁带 3
    • 减少磁带 2 上的数字 B
  3. 如果磁带 2 上的 B 不为 0,则转到步骤 2
  4. 复制从磁带 3 到磁带 1 的答案

这是应该起作用的图灵代码(磁带就像指针,小写字母,输入磁带是 i):


# At the start for 2^3
# i: 000111011110000
#       ^

_start_ -> *a = 0, start2
start2 [*i==0] -> i++, *a++ = 0, *b++ = 0, start4
start2 [*i==1] -> i++, *a++ = 1, start2
start4 [*i==0] -> *b-- = 0, b--, initc
start4 [*i==1] -> i++, *b++ = 1, start4
initc -> *c++ = 0, *c++ = 1, *c++ = 1, *c-- = 0, mult

# example
# i: 00011101111000
#              ^
# a: 001110000
#        ^
# b: 001111000
#         ^
# c: 00011000
#        ^

mult[*b==0]: lastcpy
mult[*b==1]: b--, *d++ = 0, *d++ = 1, rewa
rewa[*a==0]: a++, a++, multcpy
rewa[*a==1]: a--, rewa

multcpy[*c==1]: c++, multcpy2
multcpy[*c==0]: multcpy3
multcpy2[*a==0]: multcpy
multcpy2[*a==1]: *d++ = 1, multcpy2
multcpy3: *d-- = 0, *c = 0, cpydtoc

cpydtoc[*d==1]: d--, *c++ = 1, cpydtoc
cpydtoc[*d==0]: *c-- = 0, mult

lastcpy[*c==1]: *i++ = 1, c--, lastcpy
lastcpy[*c==0]: *i = 0, _finish_

# Should end with
# i: 00011101111011111111100
#                          ^

请检查是否有错误:)

In my own Turing pseudocode:

  1. copy input A0B to Tape 2
  2. write 000010000 to Tape 3
    • multiply the number on Tape 3 by A from Tape 2 by
      1. starting at the beginning of A
      2. writing 0 to Tape 4
        • copying number 3 => 4
        • moving forward once on Tape 3 (3++)
        • going to step 3 unless A ends
        • moving answer from Tape 4 to tape 3
    • decrement the number B on Tape 2
  3. If B on Tape 2 isn't 0, go to step 2
  4. Copy the answer from Tape 3 to Tape 1

Here's the turing code that should work (tapes are like pointers, lowercase letters, input tape is i):


# At the start for 2^3
# i: 000111011110000
#       ^

_start_ -> *a = 0, start2
start2 [*i==0] -> i++, *a++ = 0, *b++ = 0, start4
start2 [*i==1] -> i++, *a++ = 1, start2
start4 [*i==0] -> *b-- = 0, b--, initc
start4 [*i==1] -> i++, *b++ = 1, start4
initc -> *c++ = 0, *c++ = 1, *c++ = 1, *c-- = 0, mult

# example
# i: 00011101111000
#              ^
# a: 001110000
#        ^
# b: 001111000
#         ^
# c: 00011000
#        ^

mult[*b==0]: lastcpy
mult[*b==1]: b--, *d++ = 0, *d++ = 1, rewa
rewa[*a==0]: a++, a++, multcpy
rewa[*a==1]: a--, rewa

multcpy[*c==1]: c++, multcpy2
multcpy[*c==0]: multcpy3
multcpy2[*a==0]: multcpy
multcpy2[*a==1]: *d++ = 1, multcpy2
multcpy3: *d-- = 0, *c = 0, cpydtoc

cpydtoc[*d==1]: d--, *c++ = 1, cpydtoc
cpydtoc[*d==0]: *c-- = 0, mult

lastcpy[*c==1]: *i++ = 1, c--, lastcpy
lastcpy[*c==0]: *i = 0, _finish_

# Should end with
# i: 00011101111011111111100
#                          ^

Please check for errors :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文