Skip to content

Instantly share code, notes, and snippets.

@thmain
Last active August 21, 2017 01:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thmain/448a1467053f5e6c03c48a9737d511c1 to your computer and use it in GitHub Desktop.
Save thmain/448a1467053f5e6c03c48a9737d511c1 to your computer and use it in GitHub Desktop.
public class KpowerN_OnlyPositive {
public static double kPowerN(int k, int n){
if(n==0)
return 1;
double halfResult = kPowerN(k, n/2);
if(n%2==0){
return halfResult*halfResult;
}else{ //n is odd
return halfResult*halfResult*k;
}
}
public static void main(String[] args) {
int k = 4;
int n = 5;
System.out.println(k + " power " + n + " : Result: " + kPowerN(k,n));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment