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.

  • $\begingroup$ 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/… $\endgroup$
    – domotorp
    Commented Mar 27, 2013 at 17:44
  • $\begingroup$ 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. $\endgroup$
    – user2216670
    Commented Mar 27, 2013 at 17:53
  • $\begingroup$ 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. $\endgroup$
    – user2216670
    Commented Mar 27, 2013 at 19:01

1 Answer 1


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.

Pulling the above together gives an algorithm to check if your regular (or even context free language) contains strings of prime length. Definitely not a simple question, IMVHO...

  • $\begingroup$ I'd appreciate some help understanding Parikh's theorem in this instance. We can obviously turn an NFA into a PDA by just not using the stack in the PDA. Do the linear subsets specify the cycles? If so, how does that work? $\endgroup$
    – Chill
    Commented Mar 27, 2013 at 20:28
  • 1
    $\begingroup$ @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). $\endgroup$
    – vonbrand
    Commented Mar 27, 2013 at 20:39
  • 1
    $\begingroup$ I think that answers my question. I'm going to try to read up more on Parikh's theorem. I understand the idea of it and how it can specify cycles in this case. What I want to figure out is a more "hands on" solution where I make an actual algorithm to solve this problem. $\endgroup$
    – Chill
    Commented Mar 27, 2013 at 20:55
  • $\begingroup$ @Chill, just look at my previous comment. It isn't so hard to come up with a description of the possible lengths by just erasing the symbols on the DFA as a graph and checking for walks between the start start state and final states. Hard to formalize, easy to figure out by hand for any given example. $\endgroup$
    – vonbrand
    Commented Mar 27, 2013 at 21:00
  • 3
    $\begingroup$ @dkuper, it is not that simple. The regular language $a a a a (a a)^*$ is infinite, but contains no string of prime length. $\endgroup$
    – vonbrand
    Commented Mar 28, 2013 at 12:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.