3n+1 Termination?
Study the following program. Assuming that the arg[ 1] is a non-negative integer, does it terminate?
Build it. Run it a few times.
Demonstrating that in billion of such runs, it terminated each time is not good enough. Demonstrating that in one specific example run with initial value x0 for x, the loop will reassign that value to x again is a valid counter example.
1 The Code for 3n+1
// 3n+1 #include <iostream> using namespace std; #include <stdlib.h> int main(int argc, char * argv []) { int x = atoi(argv[1]); int y; cout << x; /*@ x is the given input integer, x > 0 */ while (x > 1) { cout << x << '\n'; x = (x % 2 == 0? x / 2 : 3*x + 1); } }
2 Pre- Post Conditions
- The pre-condition for the while loop is
x > 0
- The post-condition for the while loop is
!(x > 1)
3 Loop Invariant
- Can we write loop invariant(s) for this? Yes, of course.
- The trivial
true
is a loop invariant. But, too weak to be of help here. - In our course, we permitted outselves a notation through we can refer to values that a var had at the beginning of that line. E.g., x@3 stands for the value x had at the beginning of line# 3.
- Among all possible loop invariants, we choose the strongest one.
4 Termination?
- How do we show/ discuss/ argue that some loop terminates? We
discover some expression E that monotonically decreases and we know
what its least value is, say Z. Then,
Z < = E < E-prime
becomes (part of) the loop invariant. HereE-prime
stands for the value of E in an earlier iteration. - The expression E may or may not have been there in the loop originally. Just for showing properties of a piece of code, we are permitted to introduce new variables and expressions that do not otherwise influence the process state.
5 Lessons from 3n+1
- Our point #1: Simple programs can have properties that are extremely hard to prove.
- Our point #2: It is failrly easy to dress probelms from other fields (in this case, Number Theory) into problems of software.
- Our point #3: If we some how discover such masquerades, and "forbid" them: Are the rest of the properties of software "difficult"?
6 References
- The above is known as https://en.wikipedia.org/wiki/Collatz_conjecture. Recommended Reading.