-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkaratsuba.py
36 lines (26 loc) · 865 Bytes
/
karatsuba.py
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
def intMultiplcation(a, b):
return a * b
def karatsuba(a, b):
if not isinstance(a, int) or not isinstance(b, int):
raise ValueError("Both inputs must be integers")
if a == 0 or b == 0:
return 0
if a < 10 or b < 10:
return a * b
n = max(len(str(abs(a))), len(str(abs(b))))
m = n // 2
pow10m = 10 ** m
aH, aL = divmod(a, pow10m)
bH, bL = divmod(b, pow10m)
z0 = karatsuba(aL, bL)
z1 = karatsuba(aL + aH, bL + bH)
z2 = karatsuba(aH, bH)
return (z2 * 10**(2 * m)) + ((z1 - z2 - z0) * 10**m) + z0
x = 12.35
y = 1234
if not (isinstance(x, int) and isinstance(y, int)):
raise ValueError("Both inputs must be integers")
res = intMultiplcation(x, y)
print(f"Using Regular Approach: {x} * {y} = {res}")
res = karatsuba(x, y)
print(f"Using Karatsuba's Algorithm: {x} * {y} = {res}")