Pow(x, n)
Implement myPow(x, n), which computes x raised to the integer power n (x^n). The exponent n may be negative, zero, or positive. Return the result as a floating-point number.
Open official problem prompt ↗Compute x^n exactly and fast, even when n is a huge (possibly negative) integer, without performing n separate multiplications.
Doubling money: instead of adding one coin at a time, you keep doubling your pile (x, x^2, x^4, x^8, ...) and only cash in the piles whose 'bit' you actually need to reach the target power.
- Input
- x = 2.00000, n = 10
- Output
- 1024.00000
- Why
- 2 multiplied by itself 10 times equals 1024.
-100.0 < x < 100.0-2^31 <= n <= 2^31 - 1n is an integerEither x is not zero or n > 0-10^4 <= x^n <= 10^4