Skip to content

Commit 9c9a593

Browse files
author
Bhrigu Kansra
authored
Merge pull request #15 from joybanerjee08/master
Karatsuba in Python
2 parents bcc8e20 + 5215bdd commit 9c9a593

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

multiplication/karatsuba.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
def karatsuba(x,y):
2+
if len(str(x)) or len(str(y)) :
3+
return x*y
4+
else:
5+
n = max(len(str(x)),len(str(y)))
6+
z = n / 2
7+
a = x / 10**(z)
8+
b = x % 10**(z)
9+
c = y / 10**(z)
10+
d = y % 10**(z)
11+
ac = karatsuba(a,c)
12+
bd = karatsuba(b,d)
13+
adbc = karatsuba(a+b,c+d) - ac - bd
14+
return ac * 10**(2*z) + (adbc * 10**z) + bd
15+
16+
karatsuba (923,1723)

0 commit comments

Comments
 (0)