简体   繁体   English

设计一个图灵机来接受 {1^n: n is prime number}

[英]Design a turing machine to accept {1^n : n is prime number}

Design a Turing Machine to accept {1^n: n is prime number}.设计一个图灵机来接受 {1^n: n is prime number}。

I have this homework to make a recognizer Turing Machine that will be accepted if the occurrences of 1 are equal to any prime number.我有这个家庭作业来制作一个识别器图灵机,如果 1 的出现等于任何质数,它将被接受。 As of now, I still got no idea how to find the solution related to this prime number.到目前为止,我仍然不知道如何找到与这个素数相关的解决方案。

How should I go about this?我应该怎么go一下这个?

Because we're making a Turing machine and we haven't explicitly said we care about performance, odds are we just care about showing that TMs can solve this problem - so, any solution, no matter how dumb, should suffice.因为我们正在制造图灵机并且我们没有明确表示我们关心性能,所以我们很可能只关心证明 TM 可以解决这个问题 - 所以,任何解决方案,无论多么愚蠢,都应该足够了。 What is a correct, if needlessly tedious, way to show that a number in unary format (eg, 1^p) is a prime number?什么是正确的,如果不必要的乏味,表明一元格式的数字(例如,1^p)是质数的方法? One way is to check whether the number of p's is evenly divisible by any number between 2 and p - 1, inclusive.一种方法是检查 p 的数量是否可以被 2 和 p - 1(含)之间的任何数字整除。 This is actually pretty easy to do for a Turing machine.对于图灵机来说,这实际上很容易做到。 Since the problem doesn't tell us not to, we can make it even simpler by using a multi-tape Turing machine for our construction.由于问题并没有告诉我们不要这样做,我们可以通过使用多带图灵机来构造它,从而使它变得更加简单。

Let the input be on tape #1 and use tape #2 to record the current thing we are trying to divide the input by.让输入在#1 磁带上,并使用#2 磁带记录我们试图除以输入的当前事物。 Before we begin, we can verify that our p is greater than 2, as follows:在开始之前,我们可以验证一下我们的p是否大于2,如下:

  • see if the current tape square is 1, if so, move right, else halt-reject since 0 is not prime查看当前磁带方格是否为 1,如果是,则向右移动,否则停止拒绝,因为 0 不是素数
  • see if the current tape square is 1, if so, move right, else, halt-reject since 1 is not prime查看当前磁带方格是否为 1,如果是,则向右移动,否则,停止拒绝,因为 1 不是质数
  • see if the current tape square is 1, if so, reset the tape head and continue on with our division process, knowing p > 2;查看当前磁带方格是否为 1,如果是,重置磁带头并继续我们的除法过程,知道 p > 2; else, halt-accept since 2 is prime.否则,暂停接受,因为 2 是质数。

If we're continuing at this point, that means we've verified we are looking at the unary encoding of a number greater than 2. We need to do this because 2 is the first number for which we need to check divisibility, and we don't want to say 2 is composite since 2 divides it.如果我们在这一点上继续,那意味着我们已经验证我们正在查看大于 2 的数字的一元编码。我们需要这样做,因为 2 是我们需要检查整除性的第一个数字,我们不想说 2 是合数,因为 2 整除它。 At this stage, we can write 11 (unary 2) on tape #2.在这个阶段,我们可以在#2 磁带上写入 11(一元 2)。 If you like, you can do this as you are resetting the tape head as mentioned above.如果愿意,您可以在重置磁头时执行此操作,如上所述。 Otherwise, you can use some new states specifically for that part of the setup.否则,您可以使用一些专门用于该部分设置的新状态。

We are now looking at a TM configuration like this:我们现在正在查看这样的 TM 配置:

#1111111111111111111111#
 ^
#11#
 ^

We want to see if the number represented on the second tape evenly divides the number represented on the first tape.我们想看看第二张纸带上的数字是否能整除第一张纸带上的数字。 To do this, we can "cross out" numbers on the first tape repeatedly, in groups the size of the second tape, until we run out of numbers on the first tape.为此,我们可以重复“划掉”第一个磁带上的数字,分组的大小与第二个磁带的大小相同,直到我们用完第一个磁带上的数字。 If we run out in the middle of crossing out a whole group, then the number represented on the first tape is not evenly divisible by the number represented on the second tape, and we can proceed to check increasing numbers on the second tape.如果我们在划掉整个组的中间用完了,那么第一个磁带上表示的数字不能被第二个磁带上表示的数字整除,我们可以继续检查第二个磁带上的递增数字。 If we run out after having crossed out an entire group, then it is evenly divisible by a number other than 1 and itself, so we can halt-reject as the number is not prime.如果我们在划掉整个组后用完了,那么它可以被除 1 和它本身之外的数字整除,所以我们可以停止拒绝,因为这个数字不是质数。 Processing our example would look like:处理我们的示例如下所示:

=>   #1111111#   =>   #x111111#   =>   #xx11111#   =>   #xx11111#
      ^                 ^                 ^                ^
     #11#             #11#             #11#             #11#
      ^                 ^                 ^               ^

=>   #xx11111#   =>   #xx11111#   =>   #xx11111#   =>   #xxx1111#
        ^                ^                ^                 ^
     #11#             #11#             #11#             #11#
      ^               ^                 ^                 ^

=>   #xxxx111#   =>  ... reset    =>   #xxxx111#   =>   ... cross
          ^           tape 2                ^            off another
     #11#             back to          #11#              pair of 1s
        ^             head ...          ^               ...

=>   #xxxxxx1#   =>   #xxxxxxx#
            ^                 ^
     #11#             #11#
      ^                 ^

At this stage we see a 1 on the second tape and a blank on the first tape;在此阶段,我们在第二盘磁带上看到 1,在第一盘磁带上看到空白; this means the number was not divisible by our current guess.这意味着这个数字不能被我们目前的猜测整除。 If we had seen blank/blank, we could have halt-rejected immediately.如果我们看到 blank/blank,我们可以立即停止拒绝。 Instead, we need to continue checking larger possible divisors.相反,我们需要继续检查更大的可能除数。 At this stage we need to:这个阶段我们需要:

  1. reset the first tape head to the beginning of the input, replacing x's with 1's again.将第一个磁带头重置为输入的开头,再次用 1 替换 x。
  2. add an extra 1 to the second tape head to increment its value, then reset the head to the beginning of its value.将额外的 1 添加到第二个磁带头以增加其值,然后将磁头重置为其值的开头。
  3. repeat the divisibility check described above.重复上述可分性检查。

If we continue this process, we will eventually find a divisor of the number represented on the input tape, if that number is composite.如果我们继续这个过程,我们最终会找到输入磁带上表示的数字的除数,如果该数字是合数的话。 As it is, however, we will currently halt-reject on prime numbers when the second tape increases to the same number as the input.然而,实际上,当第二条磁带增加到与输入相同的数字时,我们目前将暂停拒绝素数。 We will then find that the prime number evenly divides itself.然后我们会发现质数会平分自己。 We need to check for this;我们需要检查一下; a good place would be between steps 2 and 3 in the last set of 3 steps above, we can compare tapes #1 and #2 and see if they match exactly.一个好的位置是在上面最后一组 3 个步骤中的第 2 步和第 3 步之间,我们可以比较磁带 #1 和 #2 看看它们是否完全匹配。 this would be just like the divisibility check, but it would only cross off at most one copy of tape #2 from tape #1, and it would halt-accept if it got to blank/blank rather than halt-rejecting.这就像可分性检查一样,但它最多只会从磁带 #1 中删除磁带 #2 的一个副本,如果它变成空白/空白,它会停止接受而不是停止拒绝。

Obviously, there are a lot of details to fill out if you want to formally define the TM by giving its transition table.显然,如果您想通过提供其转换表来正式定义 TM,则需要填写很多细节。 But, it can be done and a procedure like the one outlined here can be used to solve this problem.但是,这是可以做到的,并且可以使用此处概述的过程来解决此问题。 Again, this is not the most efficient way to solve this, but it is a way to solve it, which generally is good enough when looking for TMs for some problem.同样,这不是解决此问题的最有效方法,但它是解决此问题的一种方法,通常在为某些问题寻找 TM 时就足够了。

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

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