El Calkin–Wilf árbol es un árbol de fracciones de donde conseguir los dos nodos secundarios, el primer hijo (el padre del numerador / x) y el segundo niño es (x / el padre del denominador), donde x es la suma de los padres del numerador y del denominador. (Esta parte fue añadido más tarde por lo que esta pregunta es la búsqueda de poder)
Se parece a esto cuando las fracciones se expresan como coordenadas:
$$ \newcommand{\r}{→} \newcommand{\s}{\quad} \begin{align} &(1, 1) \r \\ &\s(1, 2) \r \\ &\s\s(1, 3) \r \\ &\s\s\s(1, 4) \r\ \dots \\ &\s\s\s(4, 3) \r\ \dots \\ &\s\s(3, 2) \r \\ &\s\s\s(3, 5) \r\ \dots \\ &\s\s\s(5, 2) \r\ \dots \\ &\s(2, 1) \r \\ &\s\s(2, 3) \r \\ &\s\s\s(2, 5) \r\ \dots \\ &\s\s\s(5, 3) \r\ \dots \\ &\s\s(3, 1) \r \\ &\s\s\s(3, 4) \r\ \dots \\ &\s\s\s(4, 1) \r\ \dots \end{align} $$
So, I would like to find out if there is an algorithm to find the minimum number of times the rule has to be applied to get to a certain number.
Here are some patterns I have noticed (Where you are trying to get to $(a, b)$, and the function that returns the minimum times is $f(a, b)$, which is $-1$ where it is impossible)
f(a, b) = f(b, a)
f(1, b) = b - 1
f(a, a) = -1 (except when a = 1, as f(1, 1) = 0)
f(2, b) = -1 if b is even, else ceil(b / 2)
f(<prime number>, b) = -1 iff b is a multiple of a
f(3, b) = (-1 iff (b is a multiple of 3)), else (floor(n / 3) + 2)
If you arrange them in a table (Download here)
Rows are the same columns, as a and b can be reversed.
The diagonals from the top-left to the bottom-right are the same as a row / column.
I can make some brute-force code (In Python)
def minimise(a, b):
if a < 1 > b:
return -1
possible = {(1, 1)}
target = (a, b)
i = 0
get_nexts = next_getter_factory(target)
while possible and target not in possible:
possible = {next_pos for pos in possible for next_pos in get_nexts(*pos)}
i += 1
if not possible:
return -1
return i
def next_getter_factory(target):
A, B = target
def get_nexts(a, b):
if a + b <= B:
yield (a + b, b)
if a + b <= A:
yield (a, a + b)
return get_nexts
This has the one optimization that it won't check a possibily if one of it's values is already above the target value, but there is probably some math that can make this happen in a reasonable amount of time with large input (I am expecting values up to $2^{128}$ como entrada).