-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkaratsuba.py
30 lines (27 loc) · 1015 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
'''procedure karatsuba(num1, num2)
if (num1 < 10) or (num2 < 10)
return num1*num2
/* calculates the size of the numbers */
m = max(size_base10(num1), size_base10(num2))
m2 = m/2
/* split the digit sequences about the middle */
high1, low1 = split_at(num1, m2)
high2, low2 = split_at(num2, m2)
/* 3 calls made to numbers approximately half the size */
z0 = karatsuba(low1,low2)
z1 = karatsuba((low1+high1),(low2+high2))
z2 = karatsuba(high1,high2)
return (z2*10^(2*m2))+((z1-z2-z0)*10^(m2))+(z0)'''
def karatsuba (num1, num2):
if( num1 < 10 ) | (num2 < 10):
return num1*num2
m = max(len(str(num1)), len(str(num2)))
m2 = m//2
high1 = num1 //10**m2
low1 = num1 % 10**m2
high2 = num2 //10**m2
low2 = num2 % 10**m2
z0 = karatsuba(low1,low2)
z2 = karatsuba(high1,high2)
z1 = karatsuba((low1+high1),(low2+high2))
return (z2*10**(2*m2))+((z1-z2-z0)*10**(m2))+(z0)