/** * Seek the powers of a number * x^y,y is a positive integer.The calculator can only calculate two number multiplication, cannot calculate n number multiplication at one time.
* Know: 2^5= (2^2) ^2*2; 2^6= (2^2) ^3= ((4) ^2); 2^8= (2^2) ^4= (4^2) ^2= 16^2 * Get the rule: x^y= (x^2) ^ (Y/2), define A=X^2,B=Y/2, then get shape such as: x^y= a^b; * y assumes an odd number, then the decomposition of the last multiplied by a (as above 2^6 decomposition into 4^3): x^y=a^b*x. * * Recursive solution: So every time x passes a new value. That is a. Y-Value multiplier, which is b * * @author Stone * @date 2015-7-2 a.m. 11:31:53 */public class Power {private static Long pow (long x, long Y) {if (x = = 0 | | x = = 1) {return x;} Long tx = 0;long Ty = 0;if (Y > 1) {ty = Y/2;TX = x * x; System.out.println (TX); if (y% 2 = = 0) {return pow (tx, ty);} else {return pow (tx, ty) * x;}} else {return x;}} public static void Main (string[] args) {Long result = POW (2, 10); SYSTEM.OUT.PRINTLN (result);}}
Java Recursive Solution " calculator that can only multiply by two numbers x^y" Problem