-
Notifications
You must be signed in to change notification settings - Fork 64
/
Karatsuba.java
307 lines (272 loc) · 9.56 KB
/
Karatsuba.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import java.util.Arrays;
import java.util.Scanner;
/*
* Implementation of Karatsuba's multiplication algorithm
* This code supports non-negative numeric multiplications of up to base 36 ('Z' is max permitted by alphabets)
*/
public class Karatsuba {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int base = sc.nextInt(); // base in decimal
sc.nextLine();
String x = sc.nextLine();
String y = sc.nextLine();
System.out.println(multiply(x, y, base));
sc.close();
}
/**
* Converts a numeric string into an array of int, preserving right-left order and ignoring radix
*/
private static int[] toIntArray(String num, boolean hasRadix){
int[] arr = (hasRadix)? new int[num.length()-1]: new int[num.length()];
int index = 0;
for (int i=0; i<num.length(); i++){
char character = num.charAt(i);
// if radix
if (character == '.') continue; // skip
arr[index] = parseDigit(character);
index++;
}
return arr;
}
/**
* Converts a int array to numeric string
*/
private static String intArrayToString(int[] arr){
StringBuffer sb = new StringBuffer();
for (int i: arr){
sb.append(toDigit(i));
}
if (sb.charAt(0) == '0') sb.deleteCharAt(0);
return sb.toString();
}
/**
* Driver function for KaratsubaMultiply
* Formats input parameters and output
*/
private static String multiply(String x, String y, int base){
int radix_x_Index = x.indexOf('.');
int radix_y_Index = y.indexOf('.');
// reversed (numeric) index of radix points
int radix_x_pos = (radix_x_Index == -1)? 0: x.length() - x.indexOf('.') - 1;
int radix_y_pos = (radix_y_Index == -1)? 0: y.length() - y.indexOf('.') - 1;
// convert to int arrays (ignores radixes)
int[] x_arr = (radix_x_Index == -1)? toIntArray(x, false): toIntArray(x, true);
int[] y_arr = (radix_y_Index == -1)? toIntArray(y, false): toIntArray(y, true);
// compute multiplication
int[] ans_arr = karatsubaMultiply(x_arr, y_arr, base); // by karatsuba
StringBuffer sb = new StringBuffer(intArrayToString(ans_arr));
// incorporating back the radix point
if (radix_x_pos + radix_y_pos != 0){
int radix_pos = radix_x_pos + radix_y_pos;
if (radix_pos > ans_arr.length){
char[] shiftZeros = new char[radix_pos - ans_arr.length];
Arrays.fill(shiftZeros, '0'); // string of amount number of '0's
sb.insert(0, shiftZeros);
}
sb.insert(ans_arr.length - radix_pos - 1, '.');
}
return trimZeros(sb.toString());
}
/**
* Karatsuba's multiplication algorithm (recursive)
*/
private static int[] karatsubaMultiply(int[] x, int[] y, int base){
// equalize string lengths by prepending '0's to shorter string
if (x.length > y.length)
y = shift("right", y, x.length - y.length);
else if (y.length > x.length)
x = shift("right", x, y.length - x.length);
// base case: less than or equal to 61 digits
// Reason: Karatsuba works best for multiplicands above 320 bits ~ 186 digits for decimal
if (x.length <= 141 && y.length <= 141){
return longMultiplication(x, y, base);
}
int n = x.length;
int nHalf = x.length - n/2;
int[] x1 = copyOfRange(x, 0, n/2);
int[] x0 = copyOfRange(x, n/2, x.length);
int[] y1 = copyOfRange(y, 0, n/2);
int[] y0 = copyOfRange(y, n/2, y.length);
// 3 recursive calls
int[] x1y1 = karatsubaMultiply(x1, y1, base); // x1 * y1
int[] x1x0y1y0 = karatsubaMultiply(addition(x1, x0, base), addition(y1 , y0, base), base); // (x1 + x0)*(y1 * y0)
int[] x0y0 = karatsubaMultiply(x0, y0, base); // x0 * y0
// System.out.println("x = " + x + ", y = " + y + ", n/2 = " + n/2); // check
// System.out.println("x1 = " + x1 + ", x0 = " + x0 + ", y1 = " + y1 + ", y0 = " + y0); // check
// System.out.println("x1y1 = " + x1y1 + ", x1x0y1y0 = " + x1x0y1y0 + ", x0y0 = " + x0y0); // check
// System.out.println("------------------------------"); // check
int[] part1 = shift("left", x1y1, 2*nHalf);
int[] part2 = shift("left", subtraction(subtraction(x1x0y1y0, x1y1, base), x0y0, base), nHalf);
return addition(addition(part1, part2, base), x0y0, base);
}
/**
* Long multiplication algorithm
*/
private static int[] longMultiplication(int[] x, int[] y, int base){
int[] top, bot; // top and bottom operands in long multiplication
if (x.length >= y.length){
top = x;
bot = y;
}
else{
top = y;
bot = x;
}
int[] ans = new int[x.length + y.length];
int offset = 0; // right offset when adding the rows
// for each digit in bot string from right to left
for (int i=bot.length-1; i>=0; i--){
int carryOver_product = 0; // carryover due to multiplication
int carryOver_add = 0; // carryover due to addition
// for each digit in top string from right to left
for (int j=top.length-1; j>=0; j--){
/* derive product of 2 digits */
int product = bot[i] * top[j] + carryOver_product; // multiply top digit with bottom digit and add with carryover
// System.out.println("top[j] = " + top[j] + ", bot[i] = " + bot[i] + ", carryOver = " + carryOver + ", product = " + product); // check
carryOver_product = product/base; // carry over from product
product %= base; // product after forwarding carryover
/* add product to ans[] */
int ans_index = ans.length - (top.length - j) - offset; // corresponding index in ans[]
ans[ans_index] += product + carryOver_add;
carryOver_add = ans[ans_index]/base;
ans[ans_index] = ans[ans_index]%base;
}
// handle final carryovers
if (carryOver_product != 0 || carryOver_add != 0){
// NOTE: ans[ans_index] is definitely == 0, mathematically (carryOver_product + carryOver_add < base) is guaranteed
int ans_index = ans.length - (top.length + 1) - offset;
ans[ans_index] += carryOver_product + carryOver_add;
}
offset++; // update: pad one more '0' to the right of next row
}
return ans;
}
/**
* Shifts array by amount given and in the direction stated
*/
private static int[] shift(String direction, int[] arr, int amount){
// System.out.println("[shift] numericString,amount: " + numericString + "," + amount); // check
int[] shiftedArr = new int[arr.length + amount];
// if pad right
if (direction == "left"){
for (int i=0; i<arr.length; i++){
shiftedArr[i] = arr[i];
}
}
// else if pad left
else if (direction == "right"){
for (int i=0; i<arr.length; i++){
shiftedArr[i + amount] = arr[i];
}
}
return shiftedArr;
}
/**
* Adds two int arrays of the given base
*/
private static int[] addition(int[] a, int[] b, int base){
int[] ans = new int[Math.max(a.length, b.length) + 1];
int carryOver = 0; // 0 or 1
// scans from right to left
for (int i=ans.length-1; i>=0; i--){
int sum = carryOver;
int numericPos = ans.length - i; // numeric position of ans
// if within region of a
if (numericPos <= a.length){
sum += a[a.length - numericPos];
}
// if within region of b
if (numericPos <= b.length){
sum += b[b.length - numericPos];
}
ans[i] = sum%base; // update ans
carryOver = sum/base; // update carry over
}
return ans;
}
/**
* Subtracts 'smaller' from 'greater' int arrays of the given base
* will not produce negative numbers since greater and smaller are guaranteed by caller
*/
//
private static int[] subtraction(int[] greater, int[] smaller, int base){
int borrowOver = 0; // 0 or 1
for (int i=smaller.length-1; i>=0; i--){
int index_greater = greater.length - (smaller.length - i); // corresponding index in greater
greater[index_greater] = greater[index_greater] - smaller[i] - borrowOver;
// if need to borrow over, borrowOver = 1
if (greater[index_greater] < 0){
greater[index_greater] += base;
borrowOver = 1;
}
// else if no need to borrow over, borrowOver = 0
else borrowOver = 0;
}
// handle final borrowOver
if (borrowOver != 0) greater[greater.length - smaller.length - 1] -= borrowOver;
return greater;
}
/**
* Creates subarray from the given range
*/
private static int[] copyOfRange(int[] src, int start, int end){
int[] dest = new int[end - start];
System.arraycopy(src, start, dest, 0, end - start);
return dest;
}
/**
* Use to trim leading and trailing zeros on a result string.
*/
private static String trimZeros(String input) {
int left = 0;
int right = input.length()-1;
int fp = input.indexOf('.');
if (fp == -1) {
fp = input.length();
}
while(left < fp-1) {
if (input.charAt(left) != '0')
break;
left++;
}
while (right >= fp) {
if (input.charAt(right) != '0') {
if (input.charAt(right) == '.')
right--;
break;
}
right--;
}
if (left >= fp)
return "0" + input.substring(left,right+1);
return input.substring(left,right+1);
}
/**
* Convert digit to int (for reading)
*/
private static int parseDigit(char c) {
if (c <= '9') {
return c - '0';
}
return c - 'A' + 10;
}
/**
* Convert int to digit. (for printing)
*/
private static char toDigit(int digit) {
if (digit <= 9) {
return (char)(digit + '0');
}
return (char)(digit - 10 + 'A');
}
/**
* For checking: prints out the int array
*/
@SuppressWarnings("unused")
private static void printIntArray(int[] arr){
System.out.print("[printIntArray]: ");
for (int i: arr) System.out.print(i);
System.out.println();
}
}