-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPowerFunction.java
More file actions
38 lines (30 loc) · 1.05 KB
/
PowerFunction.java
File metadata and controls
38 lines (30 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
public class PowerFunction {
public double myPow(double x, int n) {
long nLong = n; // Convert n to long to avoid overflow
// Handle negative exponent
if (nLong < 0) {
x = 1 / x;
nLong = -nLong;
}
return power(x, nLong);
}
private double power(double x, long n) {
// Base case
if (n == 0)
return 1;
double half = power(x, n / 2);
if (n % 2 == 0) {
return half * half; // If even
} else {
return half * half * x; // If odd
}
}
public static void main(String[] args) {
PowerFunction solution = new PowerFunction();
System.out.println("2^10 = " + solution.myPow(2, 10)); // 1024.0
System.out.println("2^-2 = " + solution.myPow(2, -2)); // 0.25
System.out.println("3^5 = " + solution.myPow(3, 5)); // 243.0
System.out.println("5^0 = " + solution.myPow(5, 0)); // 1.0
System.out.println("2^-3 = " + solution.myPow(2, -3)); // 0.125
}
}