Pow(x, n)
Problem Statement
Section titled “Problem Statement”Implement pow(x, n), which calculates x raised to the power n.
You need to solve this problem efficiently.
Examples
Section titled “Examples”| Input | Output | Explanation |
|---|---|---|
| Example 1 | Result 1 | Explanation for example 1 |
| Example 2 | Result 2 | Explanation for example 2 |
Constraints
Section titled “Constraints”- Constraint 1 for Pow(x, n)
- Constraint 2
- Constraint 3
Real-World Applications
Section titled “Real-World Applications”Complexity Comparison
Section titled “Complexity Comparison”| Approach | Time | Space | Best When |
|---|---|---|---|
| Fast Exponentiation | O(n) | O(1) | When applicable |
| Iterative | O(n) | O(1) | When applicable |
| Recursive | O(n) | O(1) | When applicable |
Approach 1: Fast Exponentiation
Section titled “Approach 1: Fast Exponentiation”This approach provides an efficient solution for pow(x, n).
⏱ Time O(n) Single pass 💾 Space O(1) Minimal storage
Solution Code
Section titled “Solution Code”def powx_n_fast_exponentiation(x: float, n: int) -> float: """Calculate x^n using fast exponentiation.""" if n == 0: return 1.0 if n < 0: x = 1 / x n = -n
result = 1.0 while n > 0: if n % 2 == 1: result *= x x *= x n //= 2
return result
# Test casesprint(powx_n_fast_exponentiation(2.0, 10)) # 1024.0print(powx_n_fast_exponentiation(2.1, 3)) # 9.261using namespace std;
double powxnFastExponentiation(double x, int n) { if (n == 0) return 1.0;
long long N = n; if (N < 0) { x = 1 / x; N = -N; }
double result = 1.0; while (N > 0) { if (N % 2 == 1) { result *= x; } x *= x; N /= 2; }
return result;}
int main() { cout << powxnFastExponentiation(2.0, 10) << endl; // 1024.0 return 0;}public class PowxnFastExponentiation { public static double powxnFastExponentiation(double x, int n) { if (n == 0) return 1.0;
long N = n; if (N < 0) { x = 1 / x; N = -N; }
double result = 1.0; while (N > 0) { if (N % 2 == 1) { result *= x; } x *= x; N /= 2; }
return result; }
public static void main(String[] args) { System.out.println(powxnFastExponentiation(2.0, 10)); // 1024.0 }}function powxnFastExponentiation(x, n) { if (n === 0) return 1.0;
let N = n; if (N < 0) { x = 1 / x; N = -N; }
let result = 1.0; while (N > 0) { if (N % 2 === 1) { result *= x; } x *= x; N = Math.floor(N / 2); }
return result;}
console.log(powxnFastExponentiation(2.0, 10)); // 1024.0console.log(powxnFastExponentiation(2.1, 3)); // 9.261fn powx_n_fast_exponentiation(mut x: f64, mut n: i32) -> f64 { if n == 0 { return 1.0; }
let mut N = n as i64; if N < 0 { x = 1.0 / x; N = -N; }
let mut result = 1.0; while N > 0 { if N % 2 == 1 { result *= x; } x *= x; N /= 2; }
result}
fn main() { println!("{}", powx_n_fast_exponentiation(2.0, 10)); // 1024.0 println!("{}", powx_n_fast_exponentiation(2.1, 3)); // 9.261}package main
import "fmt"import "math"
func powxnFastExponentiation(x float64, n int) float64 { if n == 0 { return 1.0 }
N := int64(n) if N < 0 { x = 1 / x N = -N }
result := 1.0 for N > 0 { if N%2 == 1 { result *= x } x *= x N /= 2 }
return result}
func main() { fmt.Println(powxnFastExponentiation(2.0, 10)) // 1024}Approach 2: Iterative
Section titled “Approach 2: Iterative”This approach provides an efficient solution for pow(x, n).
⏱ Time O(n) Single pass 💾 Space O(1) Minimal storage
Solution Code
Section titled “Solution Code”# Python Solution# Problem: powx-n# Approach: iterative
# Implementation placeholder for powx_n_iterative# This file is auto-generated and should contain the solution code.
def powx_n_iterative(): pass
if __name__ == "__main__": pass# C++ Solution# Problem: powx-n# Approach: iterative
# Implementation placeholder for powx_n_iterative# This file is auto-generated and should contain the solution code.
def powx_n_iterative(): pass
if __name__ == "__main__": pass# Java Solution# Problem: powx-n# Approach: iterative
# Implementation placeholder for powx_n_iterative# This file is auto-generated and should contain the solution code.
def powx_n_iterative(): pass
if __name__ == "__main__": pass# JavaScript Solution# Problem: powx-n# Approach: iterative
# Implementation placeholder for powx_n_iterative# This file is auto-generated and should contain the solution code.
def powx_n_iterative(): pass
if __name__ == "__main__": pass# Rust Solution# Problem: powx-n# Approach: iterative
# Implementation placeholder for powx_n_iterative# This file is auto-generated and should contain the solution code.
def powx_n_iterative(): pass
if __name__ == "__main__": pass# Golang Solution# Problem: powx-n# Approach: iterative
# Implementation placeholder for powx_n_iterative# This file is auto-generated and should contain the solution code.
def powx_n_iterative(): pass
if __name__ == "__main__": passApproach 3: Recursive
Section titled “Approach 3: Recursive”This approach provides an efficient solution for pow(x, n).
⏱ Time O(n) Single pass 💾 Space O(1) Minimal storage
Solution Code
Section titled “Solution Code”# Python Solution# Problem: powx-n# Approach: recursive
# Implementation placeholder for powx_n_recursive# This file is auto-generated and should contain the solution code.
def powx_n_recursive(): pass
if __name__ == "__main__": pass# C++ Solution# Problem: powx-n# Approach: recursive
# Implementation placeholder for powx_n_recursive# This file is auto-generated and should contain the solution code.
def powx_n_recursive(): pass
if __name__ == "__main__": pass# Java Solution# Problem: powx-n# Approach: recursive
# Implementation placeholder for powx_n_recursive# This file is auto-generated and should contain the solution code.
def powx_n_recursive(): pass
if __name__ == "__main__": pass# JavaScript Solution# Problem: powx-n# Approach: recursive
# Implementation placeholder for powx_n_recursive# This file is auto-generated and should contain the solution code.
def powx_n_recursive(): pass
if __name__ == "__main__": pass# Rust Solution# Problem: powx-n# Approach: recursive
# Implementation placeholder for powx_n_recursive# This file is auto-generated and should contain the solution code.
def powx_n_recursive(): pass
if __name__ == "__main__": pass# Golang Solution# Problem: powx-n# Approach: recursive
# Implementation placeholder for powx_n_recursive# This file is auto-generated and should contain the solution code.
def powx_n_recursive(): pass
if __name__ == "__main__": pass