CheesecakeCuber
Veteran
Hello fellow maff enthusiasts! I recently posted in the Calculator/Math thread about implementation of the Karatsuba Algorithm. Through the generous help of our friendly forum-hood Alex Lau, I was able to implement the alg successfully in Python.
Now, to help others who are curious about fast multiplication algorithms, I'm going to post an example showing the alg as simply as I can because I know that I could have used an explanation like this. Thanks for reading!
Note: If I overlooked anything in my post, please let me know and I'll get on it.
Also, my example code.
Okso Alex pointed out a possibly more efficient function for zpadding:
Now, to help others who are curious about fast multiplication algorithms, I'm going to post an example showing the alg as simply as I can because I know that I could have used an explanation like this. Thanks for reading!
Note: If I overlooked anything in my post, please let me know and I'll get on it.
Compute 17*81:
Split Step:
minLength = 1
So, split both digits into smaller packets of length 1
a = 1
b = 7
c = 8
d = 1
Coefficient Step:
co1 = a*c = 1*8 = 8
co2 = b*d = 1*7 = 7
Solve Step:
answer = (a+b)(c+d)-co1-co2
= (1+7)(8+1)-8-7
= [(8)9]-8-7
= 72-8-7
= 57
Zero Padding Step:
answer(10**minLength) + co1(10**(2*minLength)) + co2
= 57(10**1) + 8(2*10**1) + 7
= 570 + 800 + 7
= 1377
So, 17*81 = 1377
Split Step:
minLength = 1
So, split both digits into smaller packets of length 1
a = 1
b = 7
c = 8
d = 1
Coefficient Step:
co1 = a*c = 1*8 = 8
co2 = b*d = 1*7 = 7
Solve Step:
answer = (a+b)(c+d)-co1-co2
= (1+7)(8+1)-8-7
= [(8)9]-8-7
= 72-8-7
= 57
Zero Padding Step:
answer(10**minLength) + co1(10**(2*minLength)) + co2
= 57(10**1) + 8(2*10**1) + 7
= 570 + 800 + 7
= 1377
So, 17*81 = 1377
Also, my example code.
import time
import math
import random
#Karatsuba alg is cool
def splitString(string,number):
#Converts "string" variable input to string then splits for Karatsuba, where "string" is the desired string to be split and "number" is the length of the two parts after the split
string = str(string)
return [string[0:len(string)-number], string[len(string)-number:len(string)]]
def karatsubaMulti(x,y):
#Where "x" and "y" are your factors
print "Donut worry, calculating..."
minLength = min(len(str(x))/2,len(str(y))/2)
print minLength
if int(minLength) <= 1:
#Does naive alg
return x*y
else:
a = int(splitString(x,minLength)[0])
b = int(splitString(x,minLength)[1])
c = int(splitString(y,minLength)[0])
d = int(splitString(y,minLength)[1])
coefficient1 = str(karatsubaMulti(a,c))
coefficient2 = str(karatsubaMulti(b,d))
try:
answer = str((karatsubaMulti((a + b),(c + d)) - int(coefficient1) - int(coefficient2)))
except:
answer = str(((a + b)*(c + d) - int(coefficient1) - int(coefficient2)))
return int(zeroPad(coefficient1,2*minLength)) + int(zeroPad(answer,minLength)) + int(coefficient2)
testAnswer = str(karatsubaMulti(1234,5678))
print "The product of 1234 and 5678 is " + testAnswer
print "Lol no dummy, it's " + str(1234*5678)
def zeroPad(s,number):
#string = string you are padding and number is the number of zeroes
s = int(s)
return s*(10**number)
import math
import random
#Karatsuba alg is cool
def splitString(string,number):
#Converts "string" variable input to string then splits for Karatsuba, where "string" is the desired string to be split and "number" is the length of the two parts after the split
string = str(string)
return [string[0:len(string)-number], string[len(string)-number:len(string)]]
def karatsubaMulti(x,y):
#Where "x" and "y" are your factors
print "Donut worry, calculating..."
minLength = min(len(str(x))/2,len(str(y))/2)
print minLength
if int(minLength) <= 1:
#Does naive alg
return x*y
else:
a = int(splitString(x,minLength)[0])
b = int(splitString(x,minLength)[1])
c = int(splitString(y,minLength)[0])
d = int(splitString(y,minLength)[1])
coefficient1 = str(karatsubaMulti(a,c))
coefficient2 = str(karatsubaMulti(b,d))
try:
answer = str((karatsubaMulti((a + b),(c + d)) - int(coefficient1) - int(coefficient2)))
except:
answer = str(((a + b)*(c + d) - int(coefficient1) - int(coefficient2)))
return int(zeroPad(coefficient1,2*minLength)) + int(zeroPad(answer,minLength)) + int(coefficient2)
testAnswer = str(karatsubaMulti(1234,5678))
print "The product of 1234 and 5678 is " + testAnswer
print "Lol no dummy, it's " + str(1234*5678)
def zeroPad(s,number):
#string = string you are padding and number is the number of zeroes
s = int(s)
return s*(10**number)
Okso Alex pointed out a possibly more efficient function for zpadding:
def zeroPad(s,z):
s = str(s)
z = int(z)
return s + '0'.zfill(z)
s = str(s)
z = int(z)
return s + '0'.zfill(z)
Last edited: