Java Long Multiplication Integer Array
1. Use array Integer as big int => int[] = {1, 2, 3} = 123
2. Multiply two big ints
3. Theorically, integer can be arbitrary large.
Note:
mul = m*n
reminder = mul % 10;
quotation = mul / 10;
public static int[] longMulti(int[] col, int[] row){
int[] ret = null;
if(col != null && row != null){
int lc = col.length;
int lr = row.length;
int k = lc + lr;
int kInx = k - 1;
int[][] arr = new int[lc][k];
for(int c=lc-1; c >= 0; c--){
int carry = 0;
int rc = lc - 1 - c;
for(int r=lr-1; r >= 0; r--){
int n = col[c]*row[r];
int rr = lr - 1 - r;
arr[c][kInx - rc - rr] = (carry + n) % 10;
carry = (carry + n) / 10;
}
// carry
arr[c][kInx - rc - lr] = carry;
}
ret = new int[k];
int c = 0;
for(int i=k-1; i >= 0; i--){
int s = 0;
for(int j=0; j < lc; j++){
s += arr[j][i];
}
ret[i] = (c + s) % 10;
c = (c + s) / 10;
}
}
return ret;
}
public static void test0(){
Aron.beg();
int[] a = {9, 9};
int[] b = {9, 9};
int[] c = longMulti(a, b);
Aron.printArray(c);
Aron.end();
}
---------------
[9][8][0][1]
---------------