File tree Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change
1
+ from math import ceil , floor
2
+
3
+ def karatsuba_multiplier (x ,y ):
4
+ if x < 10 and y < 10 : # recussion termination condition
5
+ return x * y
6
+
7
+ n = max (len (str (x )), len (str (y )))
8
+ m = ceil (n / 2 )
9
+
10
+ a = floor (x / 10 ** m )
11
+ b = x % (10 ** m )
12
+
13
+ c = floor (y / 10 ** m )
14
+ d = y % (10 ** m )
15
+
16
+ #recursive calls
17
+ ac = karatsuba_multiplier (a ,c )
18
+ bd = karatsuba_multiplier (b ,d )
19
+ guass_trick = karatsuba_multiplier (a + b , c + d ) - ac - bd
20
+
21
+ return int (ac * (10 ** (m * 2 )) + guass_trick * (10 ** m ) + bd )
22
+
23
+
24
+
25
+
26
+
27
+ a = int (input ("Enter first number: " ))
28
+ b = int (input ("Enter second number: " ))
29
+
30
+ result = karatsuba_multiplier (a ,b )
31
+ print (result )
32
+ print (a * b )
33
+
34
+ if a * b == result :
35
+ print ("Correct" )
36
+ else :
37
+ print ("Incorrect" )
38
+
You can’t perform that action at this time.
0 commit comments