Introduction
In this challenge, we will be dealing with a certain infinite undirected graph, which I call the high divisor graph. Its nodes are the integers starting from 2. There is an edge between two nodes a < b if a divides b and a2 ≥ b. The subgraph formed by the range from 2 to 18 looks like this:
16-8 12 18
\|/ |/|
4 6 9 10 15 14
| |/ |/ |
2 3 5 7 11 13 17
It can be shown that the infinite high divisor graph is connected, so we can ask about the shortest path between two nodes.
Input and output
Your inputs are two integers a and b. You can assume that 2 ≤ a ≤ b < 1000. Your output is the length of the shortest path between a and b in the infinite high divisor graph. This means the number of edges in the path.
You may find the following fact useful: there always exists an optimal path from a to b that's first increasing and then decreasing, and only visits nodes that are strictly less than 2b2. In particular, since b < 1000 you only need to consider nodes less than 2 000 000.
Examples
Consider the inputs 3
and 32
.
One possible path between the nodes 3 and 32 is
3 -- 6 -- 12 -- 96 -- 32
This path has four edges, and it turns out there are no shorter paths, so the correct output is 4
.
As another example, an optimal path for 2
and 25
is
2 -- 4 -- 8 -- 40 -- 200 -- 25
so the correct output is 5
.
In this case, no optimal path contains the node 50 = lcm(2, 25)
.
Rules and scoring
You can write a full program or a function. The lowest byte count wins, and standard loopholes are disallowed. There are no time or memory limits, so brute forcing is allowed.
Test cases
2 2 -> 0
2 3 -> 4
2 4 -> 1
2 5 -> 5
3 5 -> 4
6 8 -> 2
8 16 -> 1
12 16 -> 2
16 16 -> 0
2 25 -> 5
3 32 -> 4
2 256 -> 3
60 77 -> 3
56 155 -> 3
339 540 -> 2
6 966 -> 4
7 966 -> 2
11 966 -> 4
2 997 -> 7
991 997 -> 4
FindShortestPath
violate the constraint about standard loopholes? If it does, just let me know and I'll delete my submission. \$\endgroup\$