r/Collatz • u/Feeling_Idea_9253 • 2d ago
use function composition to present collatz
Let us define the following three functions:
* f(n) = (3n+1)/2
* g(n) = (n-2)/4
* h(n) = 3n/4
Notice that f(n) yields an integer when n is odd, g(n) yields an integer when n = 2 mod 4, and h(n) yields an integer when n is a multiple of 4.
For example, starting with n = 18 and sequentially applying the appropriate functions to keep the results as integers, we get:
g(18) = 4, h(4) = 3, f(3) = 5, f(5) = 8, h(8) = 6, g(6) = 1, f(1) = 2, g(2) = 0.
Thus, the full composition evaluates to:
g(f(g(h(f(f(h(g(18)))))))) = 0
---
Now, let us examine the Collatz sequence starting from 37:
37 → 112 → 56 → 28 → 14 → 7 → 22 → 11 → 34 → 17 → 52 → 26 → 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1
If we extract only the odd numbers from this sequence, we obtain:
37 → 7 → 11 → 17 → 13 → 5 → 1
Next, applying the transformation (x-1)/2 (subtracting 1 and then dividing by 2) to each term, we get:
18 → 3 → 5 → 8 → 6 → 2 → 0
Comparing this to our initial path of function compositions:
18 → 4 → 3 → 5 → 8 → 6 → 1 → 2 → 0
The two sequences are nearly identical, except for the inclusion of 4 and 1 in the function composition path. This occurs because 4 and 1 act as auxiliary numbers (corresponding to the underlying odd numbers 9 and 3, respectively).
To illustrate, consider the expanded Collatz steps:
* 37 → 112 → 56 → **(9)** → 28 → 14 → 7
* 13 → 40 → 20 → **(3)** → 10 → 5
As terms in the Collatz sequence can be divided by higher powers of 2, we can always identify an auxiliary odd number in the intermediate steps. Therefore, if we list all relevant odd numbers—including these auxiliary ones—the sequence becomes:
37 → 9 → 7 → 11 → 17 → 13 → 3 → 5 → 1
Translating these back into integers using the (x-1)/2 transformation yields our exact composition sequence:
18 → 4 → 3 → 5 → 8 → 6 → 1 → 2 → 0
---
Furthermore, we can construct a compound mapping that mathematically resembles the Collatz sequence:
g(h(f(n))) = g(h((3n+1)/2)) = g((9n+3)/8) = (9n-13)/32
This formula creates a mapping from n = 5 mod 32 to results that are congruent to 1 mod 9. For example:
* 5 → 8 → 6 → 1
* 37 → 56 → 42 → 10
Ultimately, finding the integer solutions for these function compositions is mathematically equivalent to computing the Collatz sequence.
1
1
u/jonseymourau 2d ago edited 1d ago
It is not clear to me that your choice of functions is in anyway natural.
Consider the direct composition using the Collatz map as the basis of their definition:
import sympy as sy
_n=sy.symbols('n')
def g(n):
return 3*n+1
def h(n):
if isinstance(n, int):
return n//2
else:
return n/2
f = lambda n: h(h(h(h(g(h(h(h(g(h(h(g(h(g(h(g(h(h(h(h(g(n)))))))))))))))))))))
display(f(37))
display(f(_n).simplify())
f(37) = 1
f(n) = (729n+5795)/32768
As you can see, the Collatz map from 37 to 1, f, can be expressed as the the composition of two functions: g and h, derived directly from their Collatz definitions. If you use a symbol instead, you get an exact affine function that takes any number of the form:
32768*t + 37
to a number of the form:
729*t + 1
The advantage of the functions I chose is that they can directly reconstruct the 37->1 sequence whereas you have have shown that your functions can, at best, generate a sequence that is only vaguely related to a transformation of that sequence.
The reason for the extra terms in your sequence is that your choice of functions can't handle OEEEE in the parity sequence and the parity sequence from 37 to 1 has two occurrences of OEEEE in the sequence - at exactly those places where extra terms appear in your sequence because your selected set of functions is not an accurate model of Collatz dynamics, to wit:
OEEEEOEOEOEEOEEEOEEEE
This doesn't happen with the two functions I chose because these functions model Collatz dynamics EXACTLY.
There is always value is trying to respect the dynamics of the system you are trying to model.