I am implementing karatsuba multiplication.In my assignment, I have to calculate the multiplication of 2 64 digit numbers.
but while implementing it I am getting the error-
RecursionError: maximum recursion depth exceeded while calling a Python object
So my question is is the error due to my laptops limitation or is karatsuba not applicable for such large numbers?
My code is-
def karatsuba(num1,num2):
if (len(str(num1))==1 and len(str(num2))==1):
return num1*num2
else:
print(num1,num2)
a,b=int(str(num1)[:len(str(num1))//2]),int(str(num1)[len(str(num1))//2:])
c,d=int(str(num2)[:len(str(num2))//2]),int(str(num2)[len(str(num2))//2:])
#print(a,b,c,d)
res1=karatsuba(a,c)
res2=karatsuba(b,d)
res3=karatsuba(a+b,c+d)
res4=res3-res2-res1
#print(res1,res2,res4)
n=max(len(str(num1)),len(str(num2)))
#print(n)
final_result=math.pow(10,n)*res1+math.pow(10,n/2)*res4+res2
return final_result
edit-The code works for small numbers but for large numbers it gives the error-
ValueError: invalid literal for int() with base 10: ''