4

I have a homework problem that asks me to describe a program for a non deterministic Turing Machine that accepts L = {a^n: n is prime}. I'm not sure on how to go about this. do i know n? do i use the as as unary digits and count them? can i just ignore the string, and just test for the primarily of n? or are the prime values known, and thus only those cell locations are accepting states, and i can just read in the data like normal?

how should i go about this?

Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
calccrypto
  • 8,583
  • 21
  • 68
  • 99
  • You should ask your lecturer or tutor for clarification. We can try to guess what they meant (however intelligent such a guess may be) but only they know for sure. – paxdiablo Apr 12 '12 at 01:13
  • If to go by your title, then probably what's meant is that your NTM should accept any string of `a`s of prime length. So count your `a`'s, reject the string if any other symbol is seen, and when there are no more symbols, if the count is prime, accept. Looks like it. And of course no prime is "known", your tape would only contain a sequence of `a`s initially, and all the rest of it would be blank. – Will Ness Apr 12 '12 at 08:07
  • Probably better on http://programmers.stackexchange.com/ – Abizern Sep 23 '12 at 20:25
  • @JustinEthier [The homework tag is officially deprecated](http://meta.stackexchange.com/questions/147100/the-homework-tag-is-now-officially-deprecated). – Daniel Fischer Sep 25 '12 at 14:39
  • @DanielFischer - I see that now, thanks for the head's up. – Justin Ethier Sep 25 '12 at 14:54

2 Answers2

5

First you could use a memory location somewhere to flag whether the string has been found to be of prime length or not, and then do more or less what Ness suggested (although I don't really understand his answer in its entirety).

Use the Sieve of Eratosthenes. Start with a helper string of length 2, and move one right in the input string and the helper string, returning to the start of the helper string when you hit the end character of the helper string, until you hit the end character of the input string. In this way, you can see if the helper string divides the input string. Then move on to a helper string of length 3 and do the same, and so on. Only if none of the helper string lengths divide the input string length is the input string length prime. If one helper string length DOES divide the input string length, use your flag memory slot to show that. And have the algorithm check the flag memory slot, and if it's flagged, abort all processing so that the string can be rejected.

Now, at any point while iterating over the input allow a non-deterministic jump out of the inner loop so the machine can begin testing the helper string of the next length. In this way, in a sense, helper strings of all length will be being tested simultaneously, but when your flag slot is flagged, they all cease processing and reject the string.

One final problem. Strings might get accepted before (although time is sort of a non-concept here) they are found to be non-prime. If you can figure out that problem, you're one step ahead of me.

P.S. Drineas is evil

Outback
  • 542
  • 2
  • 8
  • 20
2

You can put actual unlimited Sieve of Eratosthenes to the left of your origin point, on tape.

Non-determinism allows you to have more than one transition rule for each state. So when going left in increments of n, you can at each point either a. continue going left and marking the tape, in increments of n; or b. start over from the origin point, with the next n.

Then have your starting state with two rules (perhaps after checking that all you got on tape are only as and nothing else): a. start marking multiples of 2, and b. (now assuming sieve is already in place) count your as and use the marked primes to the left of origin, to decide whether to accept.

So your initial tape, ........aaaaaaa......... would become in the end something like .c.ccccc.ccc.c.ccc.c.ccc[p]cpcpp.OaaaaaaaA...x.y.z... (with [] marking the final head position on tape).

Will Ness
  • 70,110
  • 9
  • 98
  • 181