-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMultiply.java
109 lines (92 loc) · 3.56 KB
/
Multiply.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import java.util.*;
import java.io.*;
public class Multiply {
private static int randomInt(int size) {
Random rand = new Random();
int maxval = (1 << size) - 1;
return rand.nextInt(maxval + 1);
}
public static int[] naive(int size, int x, int y) {
// YOUR CODE GOES HERE (Note: Change return statement)
int[] score = new int[2];
int[] e = new int[2];
int[] f = new int[2];
int[] g = new int[2];
int[] h = new int[2];
if (size == 1) {
score[0] = x*y;
score[1] = 1;
return score;
}
else {
int m = (int)Math.ceil(size / 2.0);
int k = (int)(x/Math.pow(2,m));
int l = (int)(x % Math.pow(2,m));
int c = (int)(y/Math.pow(2,m));
int d = (int)(y % Math.pow(2,m));
e = naive(m,k,c);
f = naive(m,l,d);
g = naive(m,l,c);
h = naive(m,k,d);
score[0] = ((int)(Math.pow(2,(2*m)))) * e[0] + ((int)(Math.pow(2,m))) * (g[0]+h[0]) + f[0];
score[1] = (e[1]+f[1]+g[1]+h[1])+3*m;
return score;
}
}
public static int[] karatsuba(int size, int x, int y) {
// YOUR CODE GOES HERE (Note: Change return statement)
int[] score = new int[2];
int[] e = new int[2];
int[] f = new int[2];
int[] g = new int[2];
if (size == 1) {
score[0] = x*y;
score[1] = 1;
return score;
}
else {
int m = (int)Math.ceil(size / 2.0);
int a = (int)(x/Math.pow(2,m));
int b = (int)(x % Math.pow(2,m));
int c = (int)(y/Math.pow(2,m));
int d = (int)(y % Math.pow(2,m));
e = karatsuba(m,a,c);
f = karatsuba(m,b,d);
g = karatsuba(m,a-b,c-d);
score[0] = ((int)(Math.pow(2,(2*m)))) * e[0] + ((int)(Math.pow(2,m))) * (e[0]+f[0]-g[0]) + f[0];
score[1] = (e[1]+f[1]+g[1])+6*m;
return score;
}
}
public static void main(String[] args){
try{
int maxRound = 20;
int maxIntBitSize = 16;
for (int size=1; size<=maxIntBitSize; size++) {
int sumOpNaive = 0;
int sumOpKaratsuba = 0;
for (int round=0; round<maxRound; round++) {
int x = randomInt(size);
int y = randomInt(size);
int[] resNaive = naive(size,x,y);
int[] resKaratsuba = karatsuba(size,x,y);
if (resNaive[0] != resKaratsuba[0]) {
throw new Exception("Return values do not match! (x=" + x + "; y=" + y + "; Naive=" + resNaive[0] + "; Karatsuba=" + resKaratsuba[0] + ")");
}
if (resNaive[0] != (x*y)) {
int myproduct = x*y;
throw new Exception("Evaluation is wrong! (x=" + x + "; y=" + y + "; Your result=" + resNaive[0] + "; True value=" + myproduct + ")");
}
sumOpNaive += resNaive[1];
sumOpKaratsuba += resKaratsuba[1];
}
int avgOpNaive = sumOpNaive / maxRound;
int avgOpKaratsuba = sumOpKaratsuba / maxRound;
System.out.println(size + "," + avgOpNaive + "," + avgOpKaratsuba);
}
}
catch (Exception e){
System.out.println(e);
}
}
}