Exponentiation by squaring raises a base to an integer power in about log2(exp) multiplications instead of the exp multiplications you get from multiplying the base by itself over and over. The trick is that squaring the base lets you halve the exponent, so 2^30 takes roughly 5 steps rather than 30. You will implement fastPower(base, exp) where exp is any integer — positive, zero, or negative. See exponentiation by squaring for background; the built-in equivalent is Math.pow, which you may not call here.
fastPower(base, exp)
// base: number (any real number)
// exp: integer (may be negative, zero, or positive)
// returns: number (base raised to exp)
fastPower(2, 10); // 1024
fastPower(10, 3); // 1000
fastPower(5, 0); // 1
fastPower(2, 30); // 1073741824
fastPower(2, -1); // 0.5 (1 / 2)
fastPower(2, -2); // 0.25 (1 / 4)
fastPower(-2, 3); // -8 (odd exponent keeps the sign)
fastPower(0, 5); // 0
O(log exp) multiplications, not O(exp). A plain loop that multiplies exp times is correct but too slow for large exponents.exp is always a whole number, but it may be negative. base can be any number, including fractions and negatives.fastPower(b, -n) equals 1 / fastPower(b, n).0 is 1. This implementation also returns 1 for fastPower(0, 0), matching Math.pow.Math.pow or use the ** operator.