I am trying to make an algorithm for the following task:
I've already Googled many times but all I could find is ideas on how to transform a matrix, how to do geometric transformations, how to transform string into another string and similar stuff.
Does anybody have any ideas?
This is the way to find the transformation with the minimum number of operations:
(EDIT: Added parenthesis)
public static void main (String [] args)
{
int a = 5, b = 23;
System.out.println (transform (a, b) + " = " + b);
}
public static String transform (int a, int b)
{
if (a == b) return "" + a;
if (b % 2 == 0 && 2 * a <= b)
{
b = b / 2;
return transform (a, b) + " * 2";
}
else
{
b = b - 1;
return "(" + transform (a, b) + " + 1)";
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With