This line from Wikipedia made me want to ask this question:

There is, however, no general procedure for determining whether an expression involving looping instructions will halt, even when humans are tasked with the inspection. The theoretical reason for this is the undecidability of the Halting Problem: there cannot exist some algorithm which determines whether any given program stops after finitely many computation steps.

Wondering what this is and what it means. Why is the Halting Problem undecidable. I don't understand, I feel like I could tell if a program terminates or not. Wondering what an example is of a program you can't tell will terminate or not.


1 Answer 1


How about the Collatz Conjecture:

def f(n):
   if (n % 2 == 0):
      return n/2
     return 3*n + 1
n = read_input()
while (n != 1):
  n = f(n)

Will this program always halt? Nobody knows! It's an open problem in mathematics. You can run it for specific instances, and if it halts, you know that it halts. But if it doesn't halt, you never know when to quit. You could run it for 1000000 steps, but maybe it halts after 1000001 steps?

We may be able to intuitively determine which programs halt. This is because we write programs in ways that make their halting obvious: we write loops over finite numbers, we try to have recursion over strictly smaller arguments.

It turns out there is a large class of programs which are guaranteed to halt, and almost everything you can think of falls into this class. So most of the time, things you're running across on a daily basis will halt. But not every program is in this class.

The Halting problem says that there is no algorithm which can take any program which halts and return whether it halts or not. It doesn't mean that, given a specific program, we can never know if it halts.


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