Description:
Implement pow(x, n), which calculates x
raised to the power n
(i.e., xn
).
Examples:
Example 1:
Input: x = 2.00000, n = 10
Output: 1024.00000
Example 2:
Input: x = 2.10000, n = 3
Output: 9.26100
Example 3:
Input: x = 2.00000, n = -2
Output: 0.25000
Explanation: 2-2 = 1/22 = 1/4 = 0.25
Solution in Python:
To implement the pow(x, n)
function, which calculates x raised to the power nnn (i.e., xn), we can use a method called Exponentiation by Squaring. This method is efficient and works in O(log n) time complexity.
Python
class Solution:
def myPow(self, x: float, n: int) -> float:
# Helper function to compute power with positive exponent
def power(x: float, n: int) -> float:
if n == 0:
return 1.0 # Base case: x^0 is always 1
half = power(x, n // 2)
if n % 2 == 0:
return half * half # If n is even, x^n = (x^(n/2))^2
else:
return half * half * x # If n is odd, x^n = (x^(n//2))^2 * x
# If n is negative, compute the positive power and take the reciprocal
if n < 0:
x = 1 / x
n = -n
return power(x, n)
Detailed Explanation:
- Helper Function:
- We define a helper function
power
that computes x raised to the power n for non-negative integers. - Base Case: If n is 0, return 1. This is because any number raised to the power of 0 is 1.
- Recursive Case:
- Compute the power for half of n using
power(x, n // 2)
. - If n is even, then xn=(xn/2)2
- If n is odd, then xn=(xn//2)2×x.
- Compute the power for half of n using
- We define a helper function
- Handling Negative Exponent:
- If n is negative, convert the problem to computing the positive power and take the reciprocal of the result. This is done by:
- Setting x to 1/x
- Making n positive by setting n to −n
- If n is negative, convert the problem to computing the positive power and take the reciprocal of the result. This is done by:
- Computing the Result:
- Call the
power
function with the modified xxx and nnn to get the result.
- Call the
This method ensures that we efficiently compute the power of a number using a logarithmic number of multiplications. It is both time-efficient and easy to understand.
Solution in Javascript:
JavaScript
/**
* @param {number} x
* @param {number} n
* @return {number}
*/
var myPow = function(x, n) {
// Helper function to compute power with positive exponent
const power = (x, n) => {
if (n === 0) {
return 1.0; // Base case: x^0 is always 1
}
let half = power(x, Math.floor(n / 2));
if (n % 2 === 0) {
return half * half; // If n is even, x^n = (x^(n/2))^2
} else {
return half * half * x; // If n is odd, x^n = (x^(n//2))^2 * x
}
};
// If n is negative, compute the positive power and take the reciprocal
if (n < 0) {
x = 1 / x;
n = -n;
}
return power(x, n);
};
Solution in Java:
Java
class Solution {
public double myPow(double x, int n) {
// Handle negative exponents by converting n to a positive and taking reciprocal of x
long N = n; // Use long to handle the case when n is Integer.MIN_VALUE
if (N < 0) {
x = 1 / x;
N = -N;
}
// Compute the result using the helper function
return power(x, N);
}
// Helper function to compute power with positive exponent
private double power(double x, long n) {
if (n == 0) {
return 1.0; // Base case: x^0 is always 1
}
double half = power(x, n / 2);
if (n % 2 == 0) {
return half * half; // If n is even, x^n = (x^(n/2))^2
} else {
return half * half * x; // If n is odd, x^n = (x^(n/2))^2 * x
}
}
}
Solution in C#:
C#
public class Solution {
public double MyPow(double x, int n) {
// Handle negative exponents by converting n to a positive and taking reciprocal of x
long N = n; // Use long to handle the case when n is Int32.MinValue
if (N < 0) {
x = 1 / x;
N = -N;
}
// Compute the result using the helper function
return Power(x, N);
}
// Helper function to compute power with positive exponent
private double Power(double x, long n) {
if (n == 0) {
return 1.0; // Base case: x^0 is always 1
}
double half = Power(x, n / 2);
if (n % 2 == 0) {
return half * half; // If n is even, x^n = (x^(n/2))^2
} else {
return half * half * x; // If n is odd, x^n = (x^(n/2))^2 * x
}
}
}