mirror of
https://github.com/trekhleb/javascript-algorithms.git
synced 2025-12-08 19:06:00 +00:00
31 lines
893 B
JavaScript
31 lines
893 B
JavaScript
/**
|
|
* Fast Powering Algorithm.
|
|
* Recursive implementation to compute power.
|
|
*
|
|
* Complexity: log(n)
|
|
*
|
|
* @param {number} base - Number that will be raised to the power.
|
|
* @param {number} power - The power that number will be raised to.
|
|
* @return {number}
|
|
*/
|
|
export default function fastPowering(base, power) {
|
|
if (power === 0) {
|
|
// Anything that is raised to the power of zero is 1.
|
|
return 1;
|
|
}
|
|
|
|
if (power % 2 === 0) {
|
|
// If the power is even...
|
|
// we may recursively redefine the result via twice smaller powers:
|
|
// x^8 = x^4 * x^4.
|
|
const multiplier = fastPowering(base, power / 2);
|
|
return multiplier * multiplier;
|
|
}
|
|
|
|
// If the power is odd...
|
|
// we may recursively redefine the result via twice smaller powers:
|
|
// x^9 = x^4 * x^4 * x.
|
|
const multiplier = fastPowering(base, Math.floor(power / 2));
|
|
return multiplier * multiplier * base;
|
|
}
|