-
Notifications
You must be signed in to change notification settings - Fork 692
/
PowerOfNumber.java
84 lines (66 loc) · 1.99 KB
/
PowerOfNumber.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
83
84
package math;
import java.math.BigDecimal;
public class PowerOfNumber {
/*
* Given a double 'x' and an integer 'n'. Write a function to calculate 'x' raised to the power 'n'.
* Eg: power (2, 5) = 32
* power (3, 4) = 81
* power (1.5, 3) = 3.375
* power (2, -2) = 0.25
*
* Runtime Complexity:
* Logarithmic, O(logn)
*
* Memory Complexity:
* Logarithmic, O(log n)
*
* A simple algorithm of this problem is to multiply 'x' by 'n' times.
*
* */
private static double powerRec(double x, int n) {
if (n == 0) return 1;
if (n == 1) return x;
double temp = powerRec(x, n / 2);
if (n % 2 == 0) {
return temp * temp;
} else {
return x * temp * temp;
}
}
protected static double power(double x, int n) {
boolean is_negative = false;
if (n < 0) {
is_negative = true;
n *= -1;
}
double result = powerRec(x, n);
if (is_negative) {
return 1 / result;
}
return result;
}
protected static double round(double value, int places) {
if (places < 0) throw new IllegalArgumentException();
BigDecimal bd = new BigDecimal(value);
bd = bd.setScale(places, BigDecimal.ROUND_HALF_UP);
return bd.doubleValue();
}
protected static double round(double value) {
return round(value, 6);
}
public static void main(String[] args) {
int n = 6;
int k = 6;
double r1 = round(power(round(n), k));
double r2 = round(Math.pow(round(n), k));
double diff = r1 - r2;
if (diff < 0) {
diff = diff * -1;
}
if (diff > 0.0000000001) {
String msg = String.format("r1 = %f, r2 = %f, difference = %f", r1, r2, diff);
System.out.println("Failed for (" + n + ", " + k + ") " + msg);
}
System.out.println(diff > 0.0000000001);
}
}