-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathPow.java
More file actions
34 lines (29 loc) · 751 Bytes
/
Pow.java
File metadata and controls
34 lines (29 loc) · 751 Bytes
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
package expression;
public class Pow extends BinaryOperation {
public Pow(CommonExpression left, CommonExpression right) {
super(left, right);
}
protected String getSign() {
return "**";
}
// (cond (= 0 b) 1 (= 1 b) a (even? b) (power (* a a) (quot b 2)) (odd? b) (* a (power a (- b 1)))))
public int compute(int x, int y) {
if (y == 0) {
return 1;
}
if (y == 1) {
return x;
}
if (y % 2 == 0) {
return compute(x * x, y / 2);
} else {
return x * compute(x, y - 1);
}
}
protected int getPriority() {
return 1;
}
protected boolean isOrderImportant() {
return true;
}
}