-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkaratsuba.py
29 lines (19 loc) · 824 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
from typing import Tuple
def karatsuba(int1: int, int2: int) -> int:
if (int_length(int1) == 1) or (int_length(int2) == 1):
return int1 * int2
n = int_length(int1)
split_idx = n // 2
a, b = split_int(int1, split_idx)
c, d = split_int(int2, split_idx)
ac = karatsuba(a, c)
bd = karatsuba(b, d)
gauss_intermediate = karatsuba((a + b), (c + d))
ad_plus_bc = gauss_intermediate - ac - bd
return (
((10 ** (2 * split_idx)) * ac) + ((10 ** (split_idx)) * ad_plus_bc) + bd
) # doesn't need to use karatsuba for multiplication because doing so will only improve lower order terms
def int_length(integer: int) -> int:
return len(str(integer))
def split_int(integer: int, idx: int) -> Tuple[int, int]:
return integer // 10 ** (idx), integer % 10 ** (idx)