I have two integers A
and B
and I want to get, WLOG say, from A
to B
with minimum numbers of the following operations:
Currently I am solving this problem using A* search where operation weights are all 1's with heuristics of 0 (Basically Dijkstra's Algorithm, I guess). But I want a better heuristics to explore less. So far, all my attempts have failed. For example, if both are positive and unequal, I tried to estimate by squaring and doubling as much as possible. I also tried to think in terms of bit strings but 111 + 1 = 1000 makes me feel like it will not be a good heuristics. I want some ideas, if not solutions, to come up with a good heuristics.
If A > B * 2^n
, you know it must take at least n steps since you can only divide by 2. Solving for n
we get ceil(log_2(A/B))
as a heuristic. This assumes A and B have the same sign.
If A < B
then we're allowed to square, so unfortunately the equation to solve is A^(2^n) < B
, or ceil(log_2(log_A(B)))
as a lower-bound. This assumes A ≥ 2.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.