-
Notifications
You must be signed in to change notification settings - Fork 154
/
Copy pathkaratsuba.py
55 lines (42 loc) · 1.13 KB
/
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import random
from math import ceil
from math import log10
def get_digits(n):
if n > 0:
digits = int(log10(n)) + 1
elif n == 0:
digits = 1
else:
digits = int(log10(-n)) + 2
return digits
def karatsuba(x, y):
# the base case for recursion
if x < 10 and y < 10:
return x * y
# n is the number of digits in the highest input number
n = max(get_digits(x), get_digits(y))
n_2 = int(ceil(n / 2.0))
n = n if n % 2 == 0 else n + 1
# split the input numbers
a, b = divmod(x, 10 ** n_2)
c, d = divmod(y, 10 ** n_2)
# applying the recursive steps
ac = karatsuba(a, c)
bd = karatsuba(b, d)
ad_bc = karatsuba((a + b), (c + d)) - ac - bd
# performs the multiplication
z2 = (10 ** n) * ac
z1 = (10 ** n_2) * ad_bc
z0 = bd
return z2 + z1 + z0
def test():
for i in range(1000):
x = random.randint(1, 10 ** 5)
y = random.randint(1, 10 ** 5)
expected = x * y
result = karatsuba(x, y)
if result != expected:
return print("failed")
return print("ok")
if __name__ == "__main__":
test()