Karatsuba Algorithm in Python
Last Updated :
08 May, 2024
Karatsuba Algorithm is a fast multiplication algorithm that efficiently multiplies large numbers by recursively breaking them down into smaller parts.
Examples:
Input: A = 5678 B = 1234
Output: 7006652
Input: A = 1456 B = 6533
Output: 9512048
Using the Naive approach, we can multiply two numeric strings in O(N2) time where N is the length of the strings. A better approach would be to used Karatsuba Algorithm to compute the product in O(N1.59) time.
Working of Karatsuba Algorithm:
Assuming that n is the power of 2, rewrite the n-digit numbers in the form of −
A = Al * 10n/2 + Ar [Al and Ar contain leftmost and rightmost n/2 digits of A]
B = Bl * 10n/2 + Br [Bl and Br contain leftmost and rightmost n/2 digits of B]
Therefore, the product A * B can also be represented as follows:
A * B = (Al * 10n/2 + Ar) * (Bl * 10n/2 + Br)
= 10n*Al*Bl + 10n/2* (Al*Br + Bl*Ar) + Ar*Br
= 10n*Al*Bl + 10n/2*((Al + Ar)*(Bl + Br) – Al*Bl – Ar*Br) + Ar*Br [since Al*Br + Bl*Ar = (Al + Ar)*(Bl + Br) – Al*Bl – Ar*Br]
Let us solve 5678 × 1234 using Karatsuba method:
That gives us,
1456 * 6533 = ((14) * 102 + 56) * ((65) * 102+ 33)
= (104 * 14 * 65) + 102 * (14 * 33 + 65 * 56) + 56 * 33
= (104 * 14 * 65) + 102 * ((14 + 56) * (65 + 33) - 14 * 65 - 56 * 33) + (56 * 33)
= 9100000 + 100 * (70 * 98 - 14 * 65 - 56 * 33) + 1848
= 9100000 + 100 * (6860 - 910 - 1848) + 1848
= 9100000 + 410200 + 1848
= 9512048
Implementation of Karatsuba Algorithm in Python:
Below is the implementation of Karatsuba Algorithm in Python:
Python
import re
# Function to find the sum of larger
# numbers represented as a string
def findSum(str1, str2):
if len(str1) > len(str2):
str1, str2 = str2, str1
result = ""
n1, n2 = len(str1), len(str2)
str1, str2 = str1.zfill(n2), str2.zfill(n2)
carry = 0
# Perform addition digit by digit from the right
for i in range(n2 - 1, -1, -1):
sum_val = (int(str1[i]) - 0) + (int(str2[i]) - 0) + carry
result = str(sum_val % 10 + 0) + result
carry = sum_val // 10
if carry:
result = str(carry + 0) + result
return result
# Function to find the difference of larger
# numbers represented as strings
def findDiff(str1, str2):
result = ""
n1, n2 = len(str1), len(str2)
str1, str2 = str1.zfill(n2), str2.zfill(n2)
carry = 0
# Perform subtraction digit by digit from the right
for i in range(n2 - 1, -1, -1):
sub = (int(str1[i]) - 0) - (int(str2[i]) - 0) - carry
if sub < 0:
sub += 10
carry = 1
else:
carry = 0
# Append the digit to the result
result = str(sub + 0) + result
return result
# Function to remove all leading 0s
# from a given string
def removeLeadingZeros(s):
pattern = "^0+(?!$)"
s = re.sub(pattern, "", s)
return s
# Function to multiply two numbers
# using the Karatsuba algorithm
def multiply(A, B):
# Base case for small numbers: perform normal multiplication
if len(A) < 10 or len(B) < 10:
return str(int(A) * int(B))
n = max(len(A), len(B))
n2 = n // 2
# Pad the numbers with leading zeros to make them equal in length
A = A.zfill(n)
B = B.zfill(n)
# Split the numbers into halves
Al, Ar = A[:n2], A[n2:]
Bl, Br = B[:n2], B[n2:]
# Recursively compute partial products and sum using Karatsuba algorithm
p = multiply(Al, Bl)
q = multiply(Ar, Br)
r = multiply(findSum(Al, Ar), findSum(Bl, Br))
r = findDiff(r, findSum(p, q))
# Combine the partial products to get the final result
return removeLeadingZeros(findSum(findSum(p + '0' * n, r + '0' * n2), q))
# Driver Code
if __name__ == "__main__":
A = "1456"
B = "6533"
# Multiply the large numbers A and B using the Karatsuba algorithm
print(multiply(A, B))
Time Complexity: O(Nlog 3) or O(N1.59), where N is the maximum among the lengths given strings A and B.
Auxiliary Space: O(N2)
Similar Reads
Kahn's Algorithm in Python Kahn's Algorithm is used for topological sorting of a directed acyclic graph (DAG). The algorithm works by repeatedly removing nodes with no incoming edges and recording them in a topological order. Here's a step-by-step implementation of Kahn's Algorithm in Python. Example: Input:Â V=6 , E = {{2, 3}
4 min read
Searching Algorithms in Python Searching algorithms are fundamental techniques used to find an element or a value within a collection of data. In this tutorial, we'll explore some of the most commonly used searching algorithms in Python. These algorithms include Linear Search, Binary Search, Interpolation Search, and Jump Search.
6 min read
Iterate over a list in Python Python provides several ways to iterate over list. The simplest and the most common way to iterate over a list is to use a for loop. This method allows us to access each element in the list directly.Example: Print all elements in the list one by one using for loop.Pythona = [1, 3, 5, 7, 9] # On each
3 min read
Iterate over a list in Python Python provides several ways to iterate over list. The simplest and the most common way to iterate over a list is to use a for loop. This method allows us to access each element in the list directly.Example: Print all elements in the list one by one using for loop.Pythona = [1, 3, 5, 7, 9] # On each
3 min read
Python DSA Libraries Data Structures and Algorithms (DSA) serve as the backbone for efficient problem-solving and software development. Python, known for its simplicity and versatility, offers a plethora of libraries and packages that facilitate the implementation of various DSA concepts. In this article, we'll delve in
15 min read
Learn DSA with Python | Python Data Structures and Algorithms This tutorial is a beginner-friendly guide for learning data structures and algorithms using Python. In this article, we will discuss the in-built data structures such as lists, tuples, dictionaries, etc. and some user-defined data structures such as linked lists, trees, graphs, etc.1. ListList is a
8 min read