The 3x+1 problem

### The 3x+1 problem

Here is an example of a Maple procedure with an `if ... then ... else` structure.

```Collatz:=proc(n::posint)
if type(n,even) then n/2;
else 3*n+1;
fi;
end;
```

This Maple code defines a function named `Collatz` of one argument n that sends n into n/2 if n is even, and sends n into 3n+1 if n is odd. The declaration `proc(n::posint)` checks that the argument n is a positive integer and issues an error message if it is not.

There is a famous unsolved problem--often called the 3x+1 problem--about the iterates of the Collatz function. Is it the case that for every positive integer n, iterating the Collatz function eventually leads to the cycle 1->4->2->1?

For example, if you start with the number 25, successive applications of the Collatz function lead to the sequence 25, 76, 38, 19, 58, 29, 88, 44, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 21. Here is a procedure `iterateCollatz` that can be used to produce this list of numbers via `iterateCollatz(25);`.

```iterateCollatz:=proc(n::posint)
local c;
c:=n;
while c>1 do
print(c);
c:=Collatz(c):
od;
end;
```

This procedure implements a `while` loop that prints a value, applies the Collatz function to the value, and iterates as long as the value exceeds 1. No one knows if this procedure is guaranteed to terminate for every positive integral input n. For more about this problem, see a paper by Jeff Lagarias.

Exercise

1. Write a Maple procedure that takes one argument, a positive integer, and returns the number of iterations of the Collatz function required to reach the value 1 from the given starting point (assuming, of course, that the number of iterations is finite). What is the result of applying this function to your telephone number?

2. Write a Maple procedure that takes one argument, a positive integer, and determines the largest number attained by repeatedly applying the Collatz function to that value (assuming, of course, that there is such a largest number). What is the result of applying this function to your student identification number?

3. Now that you have tried your hand at writing a procedure to implement the Collatz function, find out how the experts do it. Visit the Maple Application Center and search on "Collatz".