-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathChap02.question.23.NonRecursiveFastPower.java
52 lines (40 loc) · 1.79 KB
/
Chap02.question.23.NonRecursiveFastPower.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
//Chap02.question.23.NonRecursiveFastPower.java
import java.math.BigInteger;
import java.util.ArrayList;
public class Solution {
public static void main(String... args) {
System.out.println(exponentiationRecursive(BigInteger.valueOf(17), BigInteger.valueOf(36945)));
System.out.println(exponentiationNonRecursive(BigInteger.valueOf(17), BigInteger.valueOf(36945)));
}
public static BigInteger exponentiationRecursive(BigInteger x, BigInteger n) {
assert n.compareTo(BigInteger.ZERO) >= 0;
if (n.compareTo(BigInteger.ZERO) == 0) return BigInteger.ONE;
if (n.compareTo(BigInteger.ONE) == 0) return x;
BigInteger n1 = n.divide(BigInteger.valueOf(2));
BigInteger n2 = n.subtract(n1);
return exponentiationRecursive(x, n1).multiply(exponentiationRecursive(x, n2));
}
public static BigInteger exponentiationNonRecursive(BigInteger x, BigInteger n) {
assert n.compareTo(BigInteger.ZERO) >= 0;
if (n.compareTo(BigInteger.ZERO) == 0) return BigInteger.ONE;
if (n.compareTo(BigInteger.ONE) == 0) return x;
ArrayList<BigInteger> list = new ArrayList<BigInteger>();
list.add(BigInteger.ONE);
list.add(x);
BigInteger p = BigInteger.ONE;
for (int i = 2; p.compareTo(n) <= 0; i++) {
BigInteger prev = list.get(i - 1);
list.add(prev.multiply(prev));
p = p.multiply(BigInteger.valueOf(2));
}
BigInteger result = BigInteger.ONE;
int i = 1;
while (n.compareTo(BigInteger.ZERO) > 0) {
if (n.mod(BigInteger.valueOf(2)).compareTo(BigInteger.ZERO) != 0)
result = result.multiply(list.get(i));
n = n.divide(BigInteger.valueOf(2));
i++;
}
return result;
}
}