Skip to content

Instantly share code, notes, and snippets.

@micycle1
Created January 24, 2020 14:16
Show Gist options
  • Save micycle1/043b2579aca6a0337880cff3be7965c4 to your computer and use it in GitHub Desktop.
Save micycle1/043b2579aca6a0337880cff3be7965c4 to your computer and use it in GitHub Desktop.
Fast exponent Java
public static long pow2(long a, long b) {
long re = 1;
while (b > 0) {
if ((b & 1) == 1) {
re *= a;
}
b >>= 1;
a *= a;
}
return re;
}
public static long pow1(long a, long b) {
long re = 1;
while (b-- > 0) {
re *= a;
}
return re;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment