-
Notifications
You must be signed in to change notification settings - Fork 0
/
Power_x_n.java
82 lines (72 loc) · 1.42 KB
/
Power_x_n.java
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package ProjectFiles;
public class Power_x_n {
public static void main(String[] args) {
System.out.println(myPowRec(5,0));
System.out.println(myPowRec(5,5));
System.out.println(myPow(5,0));
System.out.println(myPow(5,5));
System.out.println(myPow(5,0));
System.out.println(myPow(5,3));
System.out.println(myPow(5,1));
System.out.println(myPow(34.00515,-3));
System.out.println(myPow(2,-2));
System.out.println(myPow(2,-3));
System.out.println(myPowRec(0.00001,2147483647));
}
public static double myPowRec(double x, int n) {
if(n < 0){
x = 1/x;
n = -n;
}
if(n == 0)
return 1;
if(n == 1)
return x;
if(n %2 == 0){
double result = myPowRec(x,n/2) ;
return result * result;
}
else{
double result = myPowRec(x,n/2) ;
return result * result * x;
}
}
public static double myPow(double x, int n) {
if(n < 0){
x = 1/x;
n = -n;
}
if(n == 0)
return 1;
if(n == 1)
return x;
double[] a = new double[n+1];
a[0] = 1;
a[1] = x;
int i = 2;
while(i <= n){
if(i % 2 == 0)
a[i] = a[i/2]* a[i/2];
else
a[i] = a[i/2]* a[i/2]*x;
i++;
}
return a[n];
}
private static double myPowNeg(double x, int n) {
if(n == 1)
return x;
double[] a = new double[n+1];
a[0] = 1;
a[1] = 1/x;
int i = 2;
while(i <= n){
if(i % 2 == 0)
a[i] = a[i/2]* a[i/2];
else
a[i] = a[i/2]* a[i/2]*(1/x);
i++;
}
return a[n];
}
}