Skip to content

Commit 4c0528b

Browse files
authored
add Week 1 PA
1 parent cae036d commit 4c0528b

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#version 2.7.6
2+
def karatsuba(num1, num2):
3+
num1Str = str(num1)
4+
num2Str = str(num2)
5+
if (num1 < 10) or (num2 < 10):
6+
return num1*num2
7+
8+
maxLength = max(len(num1Str), len(num2Str))
9+
splitPosition = maxLength / 2
10+
high1, low1= int(num1Str[:-splitPosition]), int(num1Str[-splitPosition:])
11+
high2, low2= int(num2Str[:-splitPosition]), int(num2Str[-splitPosition:])
12+
z0 = karatsuba(low1, low2)
13+
z1 = karatsuba((low1 + high1), (low2 + high2))
14+
z2 = karatsuba(high1, high2)
15+
16+
return (z2*10**(2*splitPosition)) + ((z1-z2-z0)*10**(splitPosition))+z0
17+
18+

0 commit comments

Comments
 (0)