Created
January 24, 2020 14:16
-
-
Save micycle1/043b2579aca6a0337880cff3be7965c4 to your computer and use it in GitHub Desktop.
Fast exponent Java
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 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