Skip to content

Instantly share code, notes, and snippets.

@kristiankime
Last active August 29, 2015 14:26
Show Gist options
  • Select an option

  • Save kristiankime/dfd39194386082e83ad2 to your computer and use it in GitHub Desktop.

Select an option

Save kristiankime/dfd39194386082e83ad2 to your computer and use it in GitHub Desktop.
// https://helloacm.com/exponentiation-by-squaring/
public static int pow(int base, int exp) {
int ret = 1;
while(exp != 0) {
if(exp % 2 == 1) {
ret *= base;
exp -= 1;
}
base *= base;
exp /= 2;
}
return ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment