-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
45 lines (42 loc) · 1.04 KB
/
test.js
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
var multiply = function (num1, num2) {
return [...num1].reverse().reduce((a, v, i) => {
let no = bigIntMultiply(num2, v) + "0".repeat(i);
return bigIntAdd([...no].reverse(), [...a].reverse());
}, "0");
};
const bigIntAdd = (bigInt1, bigInt2) => {
const lengthier = bigInt1.length >= bigInt2.length ? bigInt1 : bigInt2;
const shorter = lengthier === bigInt2 ? bigInt1 : bigInt2;
return lengthier
.reduce(
(a, v, i) => {
console.log(v, a[i], shorter[i]);
const no = +v + a[i] + shorter[i] || 0;
const over = ~~(no / 10);
a[i] = no % 10;
a[i + 1] = over;
console.log(a);
return a;
},
[0]
)
.reverse()
.join("");
};
const bigIntMultiply = (bigInt, int) => {
return [...bigInt]
.reverse()
.reduce(
(a, v, i) => {
const no = a[i] + v * int;
const over = ~~(no / 10);
a[i] = no % 10;
a[i + 1] = over;
return a;
},
[0]
)
.reverse()
.join("");
};
console.log(multiply("24", "25"));