简体   繁体   English

图灵机算法计算0并写出二进制数量

[英]Turing machine algorithm to count 0's and write how many there were in binary

I happen to need an algorithm for a turing machine that reads a string of 0's and then writes on the tape how many there were in binary. 我碰巧需要一个用于图灵机的算法,该算法读取0的字符串,然后在磁带上写入二进制文件中有多少。

I realize that in practice the machine won't actually count the 0's but I am pretty stumped as for how to do it. 我意识到在实践中,机器实际上并不会计算0,但我很难知道如何做到这一点。

I supose first of all I'd need to mark the place where the binary number starts with an X or something, then just write a 1 for the first 0 and for each of the following 0s if least significant bit is a 0 it becomes a 1 but what if it's a 1? 首先,我需要用X或其他东西来标记二进制数开始的位置,然后只为前0写一个1,如果最低有效位为0则为后面的每个0写一个它变为a 1但是如果它是1? Maybe turn it into 0 and keep going left turing all 1s into 0s until I find a 0 or blank to turn into 1? 也许把它变成0并继续左转将所有的1都变成0直到我找到0或空白变为1? Then again, in that case it's the same thing regardless of the LSB because I'd be doing the same only the 0 would be the first position... 然后再说一次,在这种情况下,无论LSB是什么都是一样的,因为我只做同样的事情,只有0才是第一个位置......

Hmm...rubber duckie... 嗯......橡皮鸭......

Suppose the input tape is #00000000000# , where the initial read position is the first 0. 假设输入磁带是#00000000000# ,其中初始读取位置是第一个0。

  1. Move right until the end # is reached, maintaining the parity of the number of 0 encountered (initially 0, then 1, then 0,...). 向右移动直到达到#结束,保持遇到的0的奇偶校验(最初为0,然后为1,然后为0,......)。 Replace the first 0 of each pair with a - . 替换第一0每对具有的- The - read are ignored and do not change the parity. -读取被忽略,不会更改奇偶校验。

  2. Write the parity to the output tape and move left (to write bits in order) 将奇偶校验写入输出磁带并向左移动(按顺序写入位)

  3. Return the input head to the left # , and goto 1. 将输入头返回到左边# ,然后转到1。

At the end of the first pass, the input tape will be #-0-0-0-0-0-# and output is 1 . 在第一遍结束时,输入磁带将是#-0-0-0-0-0-#并且输出为1

At the end of the second pass: #---0---0---# and 11 . 在第二遍结束时: #---0---0---#11

At the end of the third pass: #-------0---# and 011 . 在第三遍结束时: #-------0---#011

At the end of the fourth pass: #-----------# and 1011 . 在第四遍结束时: #-----------#1011

Edit: I concede to Bainville. 编辑:我向贝恩维尔承认。

I figured out what I wanted to do last night and it required two separate turing machines and thats probably cheating. 我弄清楚我昨晚要做什么,它需要两个独立的图灵机,这可能是作弊。

The first machine's head would run over the input tape and simply start the second machine if it scanned a 0. 第一台机器的磁头将在输入磁带上运行,如果扫描0,则只需启动第二台机器。

The second machine would simply be an adding machine and would add 1 to the current number, which is fairly easy to do, it's just long addition and you can create a state that says there is a remainder and continue moving left until you reach a zero (and replace it with 1) or find a blank spot (and create a 1). 第二台机器只是一台添加机器,并且会在当前数字上加1,这很容易做到,它只需要很长时间的添加,你可以创建一个状态,说明有一个余数并继续向左移动直到你达到零(并用1替换)或找到一个空白点(并创建一个1)。

Bainville wins my vote. 贝恩维尔赢得了我的投票。

Check out this Turing machine simulator and its binary counting sample program: 看看这个图灵机模拟器及其二进制计数样本程序:

Uber Turing Machine enables you to program the Turing machine - a universal theoretical device that can be adapted to simulate logic of any computer algorithm. Uber图灵机使您能够对图灵机进行编程 - 图灵机是一种通用的理论设备,可以适用于模拟任何计算机算法的逻辑。 With the help of this software you are able to create new algorithms, as well as edit already prepared by someone through opening and altering of them via the convenient visual IDE. 借助该软件,您可以创建新的算法,以及通过方便的可视化IDE打开和更改已经准备好的编辑。

优步图灵机截图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM