Raise a floating-point base to an integer power without calling the built-in. The trick is binary (fast) exponentiation: square the base, halve the exponent, and absorb one extra factor whenever the exponent is odd — yielding O(log n) multiplications instead of O(n).
Implement myPow(x, n) which computes x raised to the power n, where x is a float and n is a 32-bit integer (can be negative). For example: myPow(2, 10) → 1024, and myPow(2, -2) → 0.25. Calling x * x * … n times naively is too slow when n is large (up to ~2 billion).
n can be expressed in binary. Reading the bits right-to-left: each bit position doubles the base (squaring), and when a bit is 1 we multiply that power into the result. So 2¹⁰ = 2² × 2⁸ — only two multiplications instead of nine. This is fast exponentiation, also called exponentiation by squaring.n < 0, flip to x = 1/x and n = -n. Now we only deal with non-negative exponents.result = 1, base = x, exp = n.exp:exp % 2 === 1, multiply result *= base. This "absorbs" the factor for the current bit position.base = base * base (advance to the next bit position), exp = Math.floor(exp / 2) (shift bits right).result holds the answer.exp % 2 before halving; otherwise you lose the low bit and multiply the wrong factors in.The recursive form is elegant: pow(x,n) = pow(x*x, n/2) when n is even, x * pow(x*x, (n-1)/2) when odd. Stack depth is O(log n), which is fine but the iterative version uses O(1) space.
% MOD to each multiply), matrix exponentiation (Fibonacci in O(log n)), and fast multiplication(Russian peasant algorithm). Recognising "repeat an operation n times" as a signal for binary decomposition is the key transfer.210. Negative exponent? Invert x and negate n first.1▶function myPow(x: number, n: number): number {2 // Handle negative exponent: x^-n = (1/x)^n3 if (n < 0) {4 x = 1 / x;5 n = -n;6 }78 let result = 1;9 let base = x;10 let exp = n;1112 // Fast (binary) exponentiation: O(log n)13 while (exp > 0) {14 if (exp % 2 === 1) {15 result *= base; // odd exponent: absorb one factor16 }17 base *= base; // square the base18 exp = Math.floor(exp / 2); // halve the exponent19 }2021 return result;22}
function myPow(x: number, n: number): number {
// Handle negative exponent: x^-n = (1/x)^n
if (n < 0) {
x = 1 / x;
n = -n;
}
let result = 1;
let base = x;
let exp = n;
// Fast (binary) exponentiation: O(log n)
while (exp > 0) {
if (exp % 2 === 1) {
result *= base; // odd exponent: absorb one factor
}
base *= base; // square the base
exp = Math.floor(exp / 2); // halve the exponent
}
return result;
}x⁻ⁿ = (1/x)ⁿ. Inverting x and negating n lets the rest of the function handle only non-negative exponents without any branching later.result starts at 1 (the multiplicative identity). base will be squared each iteration. exp is consumed bit-by-bit.exp. When that bit is 1 (odd check), the current power of the base is "on" in the binary decomposition of n, so we absorb it into result. Then we square base to advance to the next bit position and right-shift exp.exp reaches 0, all bits have been processed and every contributing power of x has been folded into result.n's binary representation. O(1) space — purely iterative with a fixed number of variables.-n overflows when n is -2147483648. Use a BigInt or long cast before negating.pow(x, n) = pow(x*x, n/2) for even n, or x * pow(x*x, (n-1)/2) for odd. Stack depth is O(log n).% MOD after each multiply. Same loop structure; this is how cryptographic libraries compute aᵉ mod m efficiently.0⁰ = 1. The loop handles it correctly: exp = 0, loop doesn't execute, returns 1.Fib(n) in O(log n)— the same pattern, just with 2×2 matrices as the "base".function myPow(x: number, n: number): number {
// Handle negative exponent: x^-n = (1/x)^n
if (n < 0) {
x = 1 / x;
n = -n;
}
let result = 1;
let base = x;
let exp = n;
// Fast (binary) exponentiation: O(log n)
while (exp > 0) {
if (exp % 2 === 1) {
result *= base; // odd exponent: absorb one factor
}
base *= base; // square the base
exp = Math.floor(exp / 2); // halve the exponent
}
return result;
}n's binary representation. O(1) space — purely iterative with a fixed number of variables.The literal definition: xⁿ is x multiplied by itself n times. Handle a negative exponent by inverting the base first, then loop.
function myPow(x: number, n: number): number {
if (n < 0) {
x = 1 / x;
n = -n;
}
let result = 1;
for (let i = 0; i < n; i++) {
result *= x; // one factor per iteration
}
return result;
}n up to ~2³¹, this runs billions of multiplications and times out. Because xⁿ = (x²)^(n/2), squaring the base while halving the exponent processes one binary bit of n per step, cutting the count from n to log₂ n.| "implement pow / exponentiation without built-in" | fast exponentiation loop |
| repeat-multiply n times (n large) | binary decomposition: O(log n) |
| negative exponent | invert base, negate exponent, proceed normally |
| "modular exponentiation / Fibonacci in log n" | same loop, swap scalar → matrix or % MOD |
function myPow(x: number, n: number): number {
if (n < 0) { x = 1 / x; n = -n; }
let result = 1;
let base = x;
let exp = n;
while (exp > 0) {
if (exp % 2 === 1) result *= base;
base *= base;
exp = Math.floor(exp / 2);
}
return result;
}result *= base. Always: base = base², exp = ⌊exp/2⌋.myPow(x, n)?exp % 2 === 1 must come _____ the halving step exp = Math.floor(exp / 2).myPow(2, 10). How many multiplications does the fast algorithm perform?myPow(2, 0) return?