I\'ve been trying to write a simple function in Java that can calculate a number to the nth power without using loops. I then found the Math.pow(a, b) class...
Another implementation with O(Log(n)) complexity
public static long pow(long base, long exp){ if(exp ==0){ return 1; } if(exp ==1){ return base; } if(exp % 2 == 0){ long half = pow(base, exp/2); return half * half; }else{ long half = pow(base, (exp -1)/2); return base * half * half; } }