36

The halting problem states there is no algorithm that will determine if a given program halts. As a consequence, there should be programs about which we can not tell whether they terminate or not. What are the simplest (smallest) known examples of such programs?

Raphael
  • 73,212
  • 30
  • 182
  • 400
MaiaVictor
  • 4,199
  • 2
  • 18
  • 34

9 Answers9

48

A pretty simple example could be a program testing the Collatz conjecture:

$$ f(n) = \begin{cases} \text{HALT}, &\text{if $n$ is 1} \\ f(n/2), & \text{if $n$ is even} \\ f(3n+1), & \text{if $n$ is odd} \end{cases} $$

It's known to halt for $n$ up to at least $5 × 2^{60} ≈ 5.764 × 10^{18}$, but in general it's an open problem.

npostavs
  • 589
  • 6
  • 5
32

The halting problem states there is no algorithm that will determine if a given program halts. As a consequence, there should be programs about which we can not tell whether they terminate or not.

"We" are not an algorithm =) There is no general algorithm that could determine if a given program halts for every program.

What are the simplest (smallest) known examples of such programs?

Consider the following program:

n = 3
while true:
    if is_perfect(n):
            halt()
    n = n + 2

Function is_perfect checks whether n is a perfect number. It is unknown whether there are any odd perfect numbers, so we don't know whether this program halts or not.

avsmal
  • 492
  • 3
  • 7
14

You write:

The halting problem states there is no algorithm that will determine if a given program halts. As a consequence, there should be programs about which we can not tell whether they terminate or not.

This is a non-sequitur, in both directions. You succumb to a common fallacy that is worth addressing.

Given any fixed program $P$, its halting problem ("Does $P$ always halt?") is always decidable, because the answer is either "yes" or "no". Even if you can not tell which it is, you know that one of the two trivial algorithms that answer always "yes" resp. "no" solves the $P$-halting problem.

Only if you require that the algorithm should solve the Halting problem for all¹ programs can you show that no such algorithm can exist.

Now, knowing that the Halting problem is undecidable does not imply that there are any programs nobody can not prove termination or looping of. Even if you are not more powerful than a Turing machine (which is only a hypothesis, not proven fact), all we know is that no single algorithm/person can provide such proof for all programs. There may be a different person being able to decide for each program.

Some more related reading:


So you see that your actual question (as repeated below) has nothing to do with whether the halting problem is computable. At all.

What are the simplest (smallest) known examples of [programs we don't know to halt or loop]?

This in itself is a valid question; others have given good answers. Basically, you can transform every statement $S$ with unknown truth value into an example, provided it does have a truth value:

$\qquad\displaystyle g(n) = \begin{cases}1, &S \text{ true},\\ g(n+1), &\text{else}.\end{cases}$

Granted, these are not very "natural".


  1. Not necessarily all, but "many" in some sense. Infinitely many, at least.
Raphael
  • 73,212
  • 30
  • 182
  • 400
9

Given that the Busy Beaver problem is not solved for a 5-state-2-symbol Turing machine, there must be a Turing machine with only five states and only two symbols which has not been shown to halt or not when started for an empty tape. That is a very short, concise, and closed program.

not-a-user
  • 199
  • 1
  • 2
6

Any open problem regarding the existence of a number with particular properties gives rise to such a program (the one which searches for such a number). For example, take the Collatz conjecture; since we don't know if it is true, we also don't know if the following program terminates:

    n:=1;
    found:=false;
    while not found do
      s:={};
      i:=n;
      while i not in s do
        add i to s;
        if i even then i:=i/2 else i:=3i+1
      if 1 not in s then found:=true;
      n:=n+1  
Klaus Draeger
  • 2,176
  • 12
  • 17
3

Take any open problem in Number Theory and convert it into a question of whether a given program terminates. E.g. One example that works for the twin prime conjecture is:

f(x):
    while gcd(x^2 - 1, sqrt(x+1)#) != 1:
        x += 1
    return x

Clearly passing in $x \in \Bbb{N}$ will return the next twin prime average that is $\geq x$. We can't seem to prove that this algorithm will always terminate (as that immediately implies an infinitude of twin primes).

We can refactor this into a more general question. Does the following program terminate for any 3 natural numbers $x,y,z$?

 g(x,y,z):
    while gcd(xy,z) != 1:
        z *= x
        x += 1
        y += 1

If you can prove that this always terminates, then passing in certain values for $x,y,z$ similar to the first function's variable values, then you've also proven twin primes.

It is hopeless!

Daniel Donnelly
  • 628
  • 3
  • 12
1

Write a simple program that checks whether for every n, $1 ≤ n ≤ 10^{50}$, the Collatz sequence starting with n will reach the number 1 in less than a billion iterations. When it has the answer, let the program stop if the answer is "Yes", and let it loop forever if the answer is "No".

We cannot tell whether this program terminates or not. (Who is we? Let's say "we" is anyone who could add a comment to my answer). However, someone with an incredibly powerful computer might tell. Some genius mathematician might be able to tell. There might be a rather small n, say n ≈ $10^{20}$ where a billion iterations are needed; that would be in reach of someone with a lot of determination, a lot of time, and a lot of money. But right now, we cannot tell.

gnasher729
  • 32,238
  • 36
  • 56
0

Antihydra equivalent program (6-state turing machine)

Termination of the following program in arbitrary precision integer arithmetic is equivalent to the halting problem for a 6 state Turing machine with a specific (simple, explicit) initial tape condition:

int a = 8
int b = 0
while (true) {
  if (a % 2 == 0) {
    a = (a/2)*3
    b += 2
  } else {
    if (b == 0)
      break
    a = ((a - 1)/2)*3 + 1
    b -= 1
  }
}

The Turing machine was discovered in June 2024 by the Busy Beaver challenge and is believed to be hard to decide. It was called "antihydra" and is described at: https://wiki.bbchallenge.org/wiki/Antihydra Given that they proved in Coq all 5-state turing machines, this is arguably the simplest open halting problem in existence today in the "states of an equivalent Turing machine" metric.

More context at: What are very short programs with unknown halting status?

0

the question is tricky because decidability (the CS equivalent formalization/ generalization of halting problem) is associated with languages so it needs to be recast in that format. this seems to not be pointed out much, but many open problems in math/ CS can be readily converted to problems (languages) of unknown decidability. this is because of a tight correspondence between theorem proving and (un)decidability analysis. for example (somewhat like the other answer wrt odd perfect numbers), take the twin primes conjecture which dates to the Greeks (over 2 millenia ago) and is subject to major recent research advances eg by Zhang/ Tao. convert it to an algorithmic problem as follows:

Input: n. Output: Y/N there exists at least n twin primes.

the algorithm searches for twin primes and halts if it finds n of them. it is not known if this language is decidable. resolution of the twin primes problem (which asks if there are a finite or infinite number) would also resolve the decidability of this language (if it is also proven/ discovered how many there are, if finite).

another example, take the Riemann hypothesis and consider this language:

Input: n. Output: Y/N there exist at least n nontrivial zeroes of the Riemann zeta function.

the algorithm searches for nontrivial zeroes (the code is not especially complex, its similar to root finding, and there are other equivalent formulations that are relatively simple, which basically calculate sums of "parity" of all primes less than x etc) and halts if it finds n of them and again, its not known if this language is decidable and resolution is "nearly" equivalent to solving the Riemann conjecture.

now, how about an even more spectacular example? (caveat, probably more controversial as well)

Input: c: Output: Y/N there exists an O(nc) algorithm for SAT.

similarly, resolution of decidability of this language is nearly equivalent to the P vs NP problem. however there is less obvious case for "simple" code for the problem in this case.

vzn
  • 11,162
  • 1
  • 28
  • 52