Pow(x, n) - Fast Exponentiation

Medium

Description

Implement pow(x, n), which calculates x raised to the power n (i.e., xⁿ). Use fast exponentiation for efficiency.

Examples

Input:x = 2.00000, n = 10
Output:1024.00000
Explanation:

2^10 = 1024.

Input:x = 2.0, n = -2
Output:0.25
Explanation:

Works with negative numbers.

Input:x = -3.00000, n = 4
Output:81.00000
Explanation:

(-3)^4 = (-3) × (-3) × (-3) × (-3) = 81. When a negative number is raised to an even power, the result is positive.

Constraints

  • -100.0 < x < 100.0
  • -2³¹ ≤ n ≤ 2³¹ - 1

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!