Created
June 9, 2017 07:37
-
-
Save yokolet/1a6f1312a9172cebdac33f5edaa6f50f 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 PowerImpl { | |
static int power(int x, int y) { | |
// base case | |
if (y == 0) { | |
return 1; | |
} | |
int temp = power(x, y / 2); | |
if (y % 2 == 0) { | |
return temp * temp; | |
} else { | |
return x * temp * temp; | |
} | |
} | |
public static void main(String[] args) { | |
System.out.println(power(2, 10)); | |
System.out.println(power(-2, 9)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment