Metamath Proof Explorer


Theorem karatsuba

Description: The Karatsuba multiplication algorithm. If X and Y are decomposed into two groups of digits of length M (only the lower group is known to be this size but the algorithm is most efficient when the partition is chosen near the middle of the digit string), then X Y can be written in three groups of digits, where each group needs only one multiplication. Thus, we can halve both inputs with only three multiplications on the smaller operands, yielding an asymptotic improvement of n^(log_2 3) instead of n^2 for the "naive" algorithm decmul1c . (Contributed by Mario Carneiro, 16-Jul-2015) (Revised by AV, 9-Sep-2021)

Ref Expression
Hypotheses karatsuba.a
|- A e. NN0
karatsuba.b
|- B e. NN0
karatsuba.c
|- C e. NN0
karatsuba.d
|- D e. NN0
karatsuba.s
|- S e. NN0
karatsuba.m
|- M e. NN0
karatsuba.r
|- ( A x. C ) = R
karatsuba.t
|- ( B x. D ) = T
karatsuba.e
|- ( ( A + B ) x. ( C + D ) ) = ( ( R + S ) + T )
karatsuba.x
|- ( ( A x. ( ; 1 0 ^ M ) ) + B ) = X
karatsuba.y
|- ( ( C x. ( ; 1 0 ^ M ) ) + D ) = Y
karatsuba.w
|- ( ( R x. ( ; 1 0 ^ M ) ) + S ) = W
karatsuba.z
|- ( ( W x. ( ; 1 0 ^ M ) ) + T ) = Z
Assertion karatsuba
|- ( X x. Y ) = Z

Proof

Step Hyp Ref Expression
1 karatsuba.a
 |-  A e. NN0
2 karatsuba.b
 |-  B e. NN0
3 karatsuba.c
 |-  C e. NN0
4 karatsuba.d
 |-  D e. NN0
5 karatsuba.s
 |-  S e. NN0
6 karatsuba.m
 |-  M e. NN0
7 karatsuba.r
 |-  ( A x. C ) = R
8 karatsuba.t
 |-  ( B x. D ) = T
9 karatsuba.e
 |-  ( ( A + B ) x. ( C + D ) ) = ( ( R + S ) + T )
10 karatsuba.x
 |-  ( ( A x. ( ; 1 0 ^ M ) ) + B ) = X
11 karatsuba.y
 |-  ( ( C x. ( ; 1 0 ^ M ) ) + D ) = Y
12 karatsuba.w
 |-  ( ( R x. ( ; 1 0 ^ M ) ) + S ) = W
13 karatsuba.z
 |-  ( ( W x. ( ; 1 0 ^ M ) ) + T ) = Z
14 1 nn0cni
 |-  A e. CC
15 10nn0
 |-  ; 1 0 e. NN0
16 15 nn0cni
 |-  ; 1 0 e. CC
17 expcl
 |-  ( ( ; 1 0 e. CC /\ M e. NN0 ) -> ( ; 1 0 ^ M ) e. CC )
18 16 6 17 mp2an
 |-  ( ; 1 0 ^ M ) e. CC
19 14 18 mulcli
 |-  ( A x. ( ; 1 0 ^ M ) ) e. CC
20 2 nn0cni
 |-  B e. CC
21 3 nn0cni
 |-  C e. CC
22 21 18 mulcli
 |-  ( C x. ( ; 1 0 ^ M ) ) e. CC
23 4 nn0cni
 |-  D e. CC
24 19 20 22 23 muladdi
 |-  ( ( ( A x. ( ; 1 0 ^ M ) ) + B ) x. ( ( C x. ( ; 1 0 ^ M ) ) + D ) ) = ( ( ( ( A x. ( ; 1 0 ^ M ) ) x. ( C x. ( ; 1 0 ^ M ) ) ) + ( D x. B ) ) + ( ( ( A x. ( ; 1 0 ^ M ) ) x. D ) + ( ( C x. ( ; 1 0 ^ M ) ) x. B ) ) )
25 19 22 mulcli
 |-  ( ( A x. ( ; 1 0 ^ M ) ) x. ( C x. ( ; 1 0 ^ M ) ) ) e. CC
26 23 20 mulcli
 |-  ( D x. B ) e. CC
27 19 23 mulcli
 |-  ( ( A x. ( ; 1 0 ^ M ) ) x. D ) e. CC
28 22 20 mulcli
 |-  ( ( C x. ( ; 1 0 ^ M ) ) x. B ) e. CC
29 27 28 addcli
 |-  ( ( ( A x. ( ; 1 0 ^ M ) ) x. D ) + ( ( C x. ( ; 1 0 ^ M ) ) x. B ) ) e. CC
30 25 26 29 add32i
 |-  ( ( ( ( A x. ( ; 1 0 ^ M ) ) x. ( C x. ( ; 1 0 ^ M ) ) ) + ( D x. B ) ) + ( ( ( A x. ( ; 1 0 ^ M ) ) x. D ) + ( ( C x. ( ; 1 0 ^ M ) ) x. B ) ) ) = ( ( ( ( A x. ( ; 1 0 ^ M ) ) x. ( C x. ( ; 1 0 ^ M ) ) ) + ( ( ( A x. ( ; 1 0 ^ M ) ) x. D ) + ( ( C x. ( ; 1 0 ^ M ) ) x. B ) ) ) + ( D x. B ) )
31 19 21 mulcli
 |-  ( ( A x. ( ; 1 0 ^ M ) ) x. C ) e. CC
32 5 nn0cni
 |-  S e. CC
33 31 32 18 adddiri
 |-  ( ( ( ( A x. ( ; 1 0 ^ M ) ) x. C ) + S ) x. ( ; 1 0 ^ M ) ) = ( ( ( ( A x. ( ; 1 0 ^ M ) ) x. C ) x. ( ; 1 0 ^ M ) ) + ( S x. ( ; 1 0 ^ M ) ) )
34 14 18 21 mul32i
 |-  ( ( A x. ( ; 1 0 ^ M ) ) x. C ) = ( ( A x. C ) x. ( ; 1 0 ^ M ) )
35 7 oveq1i
 |-  ( ( A x. C ) x. ( ; 1 0 ^ M ) ) = ( R x. ( ; 1 0 ^ M ) )
36 34 35 eqtri
 |-  ( ( A x. ( ; 1 0 ^ M ) ) x. C ) = ( R x. ( ; 1 0 ^ M ) )
37 36 oveq1i
 |-  ( ( ( A x. ( ; 1 0 ^ M ) ) x. C ) + S ) = ( ( R x. ( ; 1 0 ^ M ) ) + S )
38 37 12 eqtri
 |-  ( ( ( A x. ( ; 1 0 ^ M ) ) x. C ) + S ) = W
39 38 oveq1i
 |-  ( ( ( ( A x. ( ; 1 0 ^ M ) ) x. C ) + S ) x. ( ; 1 0 ^ M ) ) = ( W x. ( ; 1 0 ^ M ) )
40 19 21 18 mulassi
 |-  ( ( ( A x. ( ; 1 0 ^ M ) ) x. C ) x. ( ; 1 0 ^ M ) ) = ( ( A x. ( ; 1 0 ^ M ) ) x. ( C x. ( ; 1 0 ^ M ) ) )
41 14 21 mulcli
 |-  ( A x. C ) e. CC
42 41 26 32 add32i
 |-  ( ( ( A x. C ) + ( D x. B ) ) + S ) = ( ( ( A x. C ) + S ) + ( D x. B ) )
43 7 oveq1i
 |-  ( ( A x. C ) + S ) = ( R + S )
44 20 23 8 mulcomli
 |-  ( D x. B ) = T
45 43 44 oveq12i
 |-  ( ( ( A x. C ) + S ) + ( D x. B ) ) = ( ( R + S ) + T )
46 42 45 eqtri
 |-  ( ( ( A x. C ) + ( D x. B ) ) + S ) = ( ( R + S ) + T )
47 14 20 21 23 muladdi
 |-  ( ( A + B ) x. ( C + D ) ) = ( ( ( A x. C ) + ( D x. B ) ) + ( ( A x. D ) + ( C x. B ) ) )
48 46 9 47 3eqtr2i
 |-  ( ( ( A x. C ) + ( D x. B ) ) + S ) = ( ( ( A x. C ) + ( D x. B ) ) + ( ( A x. D ) + ( C x. B ) ) )
49 41 26 addcli
 |-  ( ( A x. C ) + ( D x. B ) ) e. CC
50 14 23 mulcli
 |-  ( A x. D ) e. CC
51 21 20 mulcli
 |-  ( C x. B ) e. CC
52 50 51 addcli
 |-  ( ( A x. D ) + ( C x. B ) ) e. CC
53 49 32 52 addcani
 |-  ( ( ( ( A x. C ) + ( D x. B ) ) + S ) = ( ( ( A x. C ) + ( D x. B ) ) + ( ( A x. D ) + ( C x. B ) ) ) <-> S = ( ( A x. D ) + ( C x. B ) ) )
54 48 53 mpbi
 |-  S = ( ( A x. D ) + ( C x. B ) )
55 54 oveq1i
 |-  ( S x. ( ; 1 0 ^ M ) ) = ( ( ( A x. D ) + ( C x. B ) ) x. ( ; 1 0 ^ M ) )
56 50 51 18 adddiri
 |-  ( ( ( A x. D ) + ( C x. B ) ) x. ( ; 1 0 ^ M ) ) = ( ( ( A x. D ) x. ( ; 1 0 ^ M ) ) + ( ( C x. B ) x. ( ; 1 0 ^ M ) ) )
57 14 23 18 mul32i
 |-  ( ( A x. D ) x. ( ; 1 0 ^ M ) ) = ( ( A x. ( ; 1 0 ^ M ) ) x. D )
58 21 20 18 mul32i
 |-  ( ( C x. B ) x. ( ; 1 0 ^ M ) ) = ( ( C x. ( ; 1 0 ^ M ) ) x. B )
59 57 58 oveq12i
 |-  ( ( ( A x. D ) x. ( ; 1 0 ^ M ) ) + ( ( C x. B ) x. ( ; 1 0 ^ M ) ) ) = ( ( ( A x. ( ; 1 0 ^ M ) ) x. D ) + ( ( C x. ( ; 1 0 ^ M ) ) x. B ) )
60 55 56 59 3eqtri
 |-  ( S x. ( ; 1 0 ^ M ) ) = ( ( ( A x. ( ; 1 0 ^ M ) ) x. D ) + ( ( C x. ( ; 1 0 ^ M ) ) x. B ) )
61 40 60 oveq12i
 |-  ( ( ( ( A x. ( ; 1 0 ^ M ) ) x. C ) x. ( ; 1 0 ^ M ) ) + ( S x. ( ; 1 0 ^ M ) ) ) = ( ( ( A x. ( ; 1 0 ^ M ) ) x. ( C x. ( ; 1 0 ^ M ) ) ) + ( ( ( A x. ( ; 1 0 ^ M ) ) x. D ) + ( ( C x. ( ; 1 0 ^ M ) ) x. B ) ) )
62 33 39 61 3eqtr3ri
 |-  ( ( ( A x. ( ; 1 0 ^ M ) ) x. ( C x. ( ; 1 0 ^ M ) ) ) + ( ( ( A x. ( ; 1 0 ^ M ) ) x. D ) + ( ( C x. ( ; 1 0 ^ M ) ) x. B ) ) ) = ( W x. ( ; 1 0 ^ M ) )
63 62 44 oveq12i
 |-  ( ( ( ( A x. ( ; 1 0 ^ M ) ) x. ( C x. ( ; 1 0 ^ M ) ) ) + ( ( ( A x. ( ; 1 0 ^ M ) ) x. D ) + ( ( C x. ( ; 1 0 ^ M ) ) x. B ) ) ) + ( D x. B ) ) = ( ( W x. ( ; 1 0 ^ M ) ) + T )
64 24 30 63 3eqtri
 |-  ( ( ( A x. ( ; 1 0 ^ M ) ) + B ) x. ( ( C x. ( ; 1 0 ^ M ) ) + D ) ) = ( ( W x. ( ; 1 0 ^ M ) ) + T )
65 10 11 oveq12i
 |-  ( ( ( A x. ( ; 1 0 ^ M ) ) + B ) x. ( ( C x. ( ; 1 0 ^ M ) ) + D ) ) = ( X x. Y )
66 64 65 13 3eqtr3i
 |-  ( X x. Y ) = Z