File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change 1+ var multiply = function ( num1 , num2 ) {
2+ return [ ...num1 ] . reverse ( ) . reduce ( ( a , v , i ) => {
3+ let no = bigIntMultiply ( num2 , v ) + "0" . repeat ( i ) ;
4+ return bigIntAdd ( [ ...no ] . reverse ( ) , [ ...a ] . reverse ( ) ) ;
5+ } , "0" ) ;
6+ } ;
7+
8+ const bigIntAdd = ( bigInt1 , bigInt2 ) => {
9+ const lengthier = bigInt1 . length >= bigInt2 . length ? bigInt1 : bigInt2 ;
10+ const shorter = lengthier === bigInt2 ? bigInt1 : bigInt2 ;
11+ return lengthier
12+ . reduce (
13+ ( a , v , i ) => {
14+ console . log ( v , a [ i ] , shorter [ i ] ) ;
15+ const no = + v + a [ i ] + shorter [ i ] || 0 ;
16+ const over = ~ ~ ( no / 10 ) ;
17+ a [ i ] = no % 10 ;
18+ a [ i + 1 ] = over ;
19+ console . log ( a ) ;
20+ return a ;
21+ } ,
22+ [ 0 ]
23+ )
24+ . reverse ( )
25+ . join ( "" ) ;
26+ } ;
27+
28+ const bigIntMultiply = ( bigInt , int ) => {
29+ return [ ...bigInt ]
30+ . reverse ( )
31+ . reduce (
32+ ( a , v , i ) => {
33+ const no = a [ i ] + v * int ;
34+ const over = ~ ~ ( no / 10 ) ;
35+ a [ i ] = no % 10 ;
36+ a [ i + 1 ] = over ;
37+ return a ;
38+ } ,
39+ [ 0 ]
40+ )
41+ . reverse ( )
42+ . join ( "" ) ;
43+ } ;
44+
45+ console . log ( multiply ( "24" , "25" ) ) ;
You can’t perform that action at this time.
0 commit comments