8

11 days ago I asked a similar question with a specific narrow focus, to which there is probably no answer. So here I am asking about the underlying problem.


I teach computer science and would like to discuss Turing's halting problem1 with my students. As an introduction to this topic, I would like to give them two computer programs (written in Python) with these properties:

  • The two programs should be as similar as possible. Ideally, they should differ only by one numeric literal, and this difference should be very small.
  • The two programs should not read any input. All parameters are defined within the program.
  • The two programs should be very short and easy to understand. The shorter the better. No more than 10 lines of code (not counting comments and blank lines).

The students are asked to tell which of the two programs halts and which runs forever. They should suggest methods how to find out the right solution.

I think a good approach is to code a math problem that has a solution for some small input parameter with a large number as the result, while having no solution at all for another small input parameter.

My first attempt was a Python program that computes the greatest common divisor of some simple expressions in a loop, halting after about $8\cdot10^{51}$ cycles:

import math
n = 1
gcd = 1
while gcd == 1:
    n += 1
    gcd = math.gcd(n ** 17 + 9, (n + 1) ** 17 + 9)
print("n   =",n)
print("gcd =",gcd)

So this program tries to find in a brute force approach the smallest $n$ for which

$$ \mathrm{gcd}(n^a+b,(n+c)^a+b)\ne1 $$

with $(a,b,c)=(17,9,1)$. (Compare OEIS A255859)

The print commands are not really necessary, so the program has 6 relevant lines of code and would run on an average common desktop computer for about $10^{41}$ years (that's about $7\cdot10^{29}$ times the age of the universe) and then it would output two numbers with 52 decimal places each.

This program is short and easy to understand. More importantly, without knowing the solution in advance and without advanced mathematical knowledge, it is virtually impossible to tell whether or not this program will eventually come to an end.

In the question linked to in the introduction, I asked for another value triple for $(a,b,c)$ that would cause the program to run forever. This would give the perfect never-stopping sibling program for the program shown here. But it seems that all non-trivial triples ($a\ne1\land c\ne1$) would cause the program to come to an end sooner or later.

Therefore, I need another mathematical problem that can be wrapped into two similar Python programs, one of which stops after a very long runtime, while the other runs forever.

What mathematical problems could these be, and with what parameters?

Just for your information:
My students are not studying math or computer science. Some of them study data science, the others information security. So most of them are interested in math, but it is not their major.


1 Turing's Halting Problem in a nutshell: Is it possible to write a computer program that is a universal halting tester (UHT)? This UHT should be able to read any computer program and the input given to that program, and then tell whether the program with that input will halt (stop) at some point or whether it will keep running forever. The answer is: It is possible to write halting testers for many special cases, but it can be proved that the existence of a universal halting tester is impossible.


Addendum (reaction to answers given so far):

Thank you very much for the answers given so far. Most of them are really great! Thank you! But it would be really nice to have a problem, that has another property that I didn't mention explicitly:

When you add a print command inside the loop of the program shown here in my question, that prints the value of gcd in each round, then you get a list with more than $8\cdot10^{51}$ identical lines, each line showing the number $1$. And then, in its last round it for the very first time prints another number (which in this case has 52 decimal digits) and then halts.

I know, I didn't mention this property explicitly, but is there another problem that fulfills all criteria form above plus the additional criterion that it repeats the very same number in many rounds before it does something else?

  • How about: A variation of the Collatz conjecture that "obviously" (always) halts vs a variation that "obviously" (always) doesn't ? – Adam Rubinson Jan 12 '23 at 10:40
  • Slight remark : The age of the universe is infinity. Not quite sure what exactly the question is but the busy beaver function shows that halting machines can take incredibly long , even for a small number of states. We know $$bb(7)>10\uparrow \uparrow 5$$ a power tower of $5$ tens. The running time of the halting champion for seven states is therefore already indistinguishable from the eternity. – Peter Jan 12 '23 at 10:40
  • @AdamRubinson: The point is exactly that it should NOT be obvious at first sight whether a program stops or not. But there would have to be some proof. – Hubert Schölnast Jan 12 '23 at 11:19
  • 1
    Ok well then replace "obvious" with "non-obvious" in my original comment. There should be such problems... – Adam Rubinson Jan 12 '23 at 11:20
  • @Peter The age of the universe is about $1.38\cdot 10^{10}$ years, which is clearly shorter than infinity. Also $10\uparrow\uparrow 5$ is significantly smaller than infinity. But I think that a duration comparable to the age of the universe is sufficient. I've thought of busy beavers before, but I don't know of any busy beavers that can be proven to run forever, and it's hard to tell if that's the case. – Hubert Schölnast Jan 12 '23 at 11:20
  • The big bang theory is flawed , so the universe actually has infinite age 2. You seem to have misunderstood my statetement that we cannot distinguish $10 \uparrow \uparrow 5$ seconds from the eternity. This was not meant mathematically and maybe unlucky formulated. 3. Of course , a busy beaver machine must halt. A machine running forever does not qualify for a busy beaver.
  • – Peter Jan 12 '23 at 13:10
  • The busy beaver function grows asymptotically faster than any computable function. I do not remember exactly for which smallest $n$ $bb(n)$ is known to be larger than Graham's number. It was something around $n=20$.
  • – Peter Jan 12 '23 at 13:13
  • @Peter: 1. I am not a coslomogy expert, and you probably are not either, so I don't want to argue with you about cosmology. 4. Two years ago I wrote a script about Busy Beaver for my students. In it I did not record the running times, but the number of ones, which such a machine writes on the tape before it stops. At that time I wrote: $f(1)=1, f(2)=4, f(3)=6, f(4)=13, f(5)\geq4089, f(6)\ge3.515\cdot10^{18267}, f(7)>10^{10^{10^{10^{18705353}}}}$ I am interested in more accurate numbers if you have some. – Hubert Schölnast Jan 12 '23 at 13:44
  • 2
    @HubertSchölnast Last year there were some big improvements: $f(6)>10\uparrow\uparrow 15.$ Also see https://en.wikipedia.org/wiki/Busy_beaver#Exact_values_and_lower_bounds – r.e.s. Jan 12 '23 at 13:55
  • @r.e.s. Wow , I never noticed those improvements. That is an incredible leap ! – Peter Jan 12 '23 at 15:15
  • @HubertSchölnast It's a shame you require Python, as Turing programs are in a perfectly good language of their own :-). E.g. this program halts after more than $10\uparrow\uparrow 15$ steps: (1R2 0L4)(1R3 0R6)(1L3 1L1)(0L5 1R0)(1L6 0R2)(0R3 0R5); but this one never halts: (0R2 0L4)(1R3 0R6)(1L3 1L1)(0L5 1R0)(1L6 0R2)(0R3 0R5). I guess it's too obvious that the second one never halts, but something like this might work. – r.e.s. Jan 12 '23 at 15:25
  • 2
    @r.e.s.: My students are not really familiar with Turing machines. (They study data science or information security at a university for applied sciences.) I explain Turing machines to them, but for them these machines feel synthetic and theoretical. They want examples that are closer to their daily life. – Hubert Schölnast Jan 12 '23 at 15:39