Fast exponentiation
Java Algorithms
binary exponentiation
Flowchart (ISO 5807)
Source code
class Power {
static long fastPow(long base, int exp) {
long result = 1;
while (exp > 0) {
if (exp % 2 == 1) {
result = result * base;
}
base = base * base;
exp = exp / 2;
}
return result;
}
}