# Can a Turing Machine decide if an NFA accepts a string of prime length?

I want to know if the following problem is decidable:

Instance: An NFA A with n states

Question: Does there exist some prime number p such that A accepts some string of length p.

My belief is that this problem is undecidable, but I can't prove it. The decider can easily have an algorithm to figure out if a particular number is prime, but I don't see how it would be able to analyze the NFA in enough detail to know exactly what lengths it can produce. It could start testing strings with the NFA, but for an infinite language, it may never halt (and thus not be a decider).

The NFA can easily be changed to a DFA or regular expression if the solution needs it, of course.

This question is something I've been pondering as a self-made prep question for a final I have coming up in 2 weeks.

• I am not sure if this is undergrad-level, so don't worry about deleting it. It might turn out to be a hard problem, see e.g. terrytao.wordpress.com/2007/05/25/… – domotorp Mar 27 '13 at 17:44
• Well, I made it up, so it may well be difficult. I haven't found any proofs of undecidable problems involving NFAs/DFAs, which is why I thought it might be interesting to try one. – user2216670 Mar 27 '13 at 17:53
• I believe what you linked to is a different (easier) problem. It can answer "how many strings of length x does an NFA accept?". Using the formula provided, we would have to check infinitely many instances of $s_L(n)$ to see if there exists a string the NFA accepts that is prime in length. I'm not asking about a particular prime, I'm asking about all of them. – user2216670 Mar 27 '13 at 19:01

The lengths of the strings accepted by a DFA form a semilinear set (like in Parikh's theorem for context free languages), the description of those isn't too hard to come by (essentially splice up all possible cycles of the automaton), and by Dirichlet's theorem any arithmetic progression of the form $a + b k$ with $\gcd(a, b) = 1$ contains an infinitude of primes.
• @Chill, Consider any path through the DFA. It might go straight from starting state to final state, or it might loop. The possible lengths of strings are determined by the "straight portion" + a sum of $k$ times "length of a possible loop" for arbitrary $k$s. Just draw some tangle of a DFA, and trace the paths through it. You will see the possible lengths fall into families of arithmetic sequences defined by the cycles, i.e., they form a semilinear set. No need to go context free (just a nice free bonus). – vonbrand Mar 27 '13 at 20:39
• @dkuper, it is not that simple. The regular language $a a a a (a a)^*$ is infinite, but contains no string of prime length. – vonbrand Mar 28 '13 at 12:17