Last active
August 21, 2017 01:06
-
-
Save thmain/448a1467053f5e6c03c48a9737d511c1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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