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?
(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