Skip to content

Instantly share code, notes, and snippets.

@yokolet
Created June 9, 2017 07:37
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 yokolet/1a6f1312a9172cebdac33f5edaa6f50f to your computer and use it in GitHub Desktop.
Save yokolet/1a6f1312a9172cebdac33f5edaa6f50f to your computer and use it in GitHub Desktop.
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