Java Program to Implement the Karatsuba Multiplication Algorithm
Last Updated :
14 Aug, 2024
It is a replacement for the algorithm that we have used since childhood, which is mainly for multiplying numbers of bigger digits. The Karatsuba Algorithm is used for the fast multiplication of large numbers, using a famous technique called as the Divide and Conquer ,developed by Anatolii Alexeevitch Karatsuba in 1960. Before entering into the topic, lets see why and when it was developed.
In total there are 3 ways to multiply numbers :
- Third-grade school algorithm Method (Standard-way)
- Recursive algorithm Method
- Karatsuba Multiplication Method
Why the Karatsuba algorithm?
The goal of the algorithm is to provide a high rich design space. Its time complexity is as follows :
 O(n^log2(3)) time (~ O(n^1.585))
Where n is the number of digits of the numbers multiplying. It is discussed by multiplying two big integer numbers to show internal working step by step. The goal is to reduce the space complexity for which the integer numbers terms will be broken down in such a way to x and y are broken into a set of digits as the logic behind it is divide and conquer. If the numbers are smaller there is no need to multiply, standard mutilation of two integers is preferred.
In this article we have standardized the algorithm to work for 4 digits , mainly for the sake of understanding. One can multiply as many digits taken in sets.
Steps involved in this algorithm:
- If x < 10 or y < 10, return x * y.
- Compute halfLength = max(length(x), length(y)) / 2.
- Split x into max1 = x / 10^halfLength and min1 = x % 10^halfLength.
- Split y into max2 = y / 10^halfLength and min2 = y % 10^halfLength.
- Compute ac = karatsubaMultiply(max1, max2).
- Compute bd = karatsubaMultiply(min1, min2).
- Compute ab_cd = karatsubaMultiply(max1 + min1, max2 + min2).
- Compute result = (ac * 10^(2 * halfLength)) + ((ab_cd - ac - bd) * 10^halfLength) + bd.
Now lets see the illustration of the above steps in detail , before entering into the result.
Illustration of the Steps:
Let the Inputs be: x = 1234, y = 5678

Given:
x = 1234
y = 5678
Step 1: Split `x` and `y` into two parts:
a = 12, b = 34
c = 56, d = 78
Step 2: Calculate the products:
a * c = 12 * 56 = 672
b * d = 34 * 78 = 2652
Step 3: Calculate the sum of `(a + b)` and `(c + d)`:
(a + b) = 12 + 34 = 46 // you are taking 36
(c + d) = 56 + 78 = 134
Step 4: Calculate the product of `(a + b)` and `(c + d)`:
(a + b) * (c + d) = 46 * 134 = 6164
Step 5: Calculate the final result:
Result = (a * c) * 10000 + (b * d) + (a + b) * (c + d)
Result = 672 * 10000 + 2652 + 6164
Result = 6720000 + 2652 + 6164
Result = 6728816
Output: 6728816
Program Implementation:
Note not to grasp the formulas laid down for this algorithm rather understanding it in this way makes it easier to implement.
Java
public class KaratsubaMultiplication {
// Method to perform Karatsuba multiplication
public static long karatsubaMultiply(long x, long y) {
// Base case: if either number is small, use standard multiplication
if (x < 10 || y < 10) {
return x * y;
}
// Determine the length of the numbers
int length = Math.max(Long.toString(x).length(), Long.toString(y).length());
int halfLength = length / 2;
// Split the numbers into two halves
long max1 = x / (long) Math.pow(10, halfLength);
long min1 = x % (long) Math.pow(10, halfLength);
long max2 = y / (long) Math.pow(10, halfLength);
long min2 = y % (long) Math.pow(10, halfLength);
// Compute ac = max1 * max2
long ac = karatsubaMultiply(max1, max2);
// Compute bd = min1 * min2
long bd = karatsubaMultiply(min1, min2);
// Compute (a+b)(c+d) - ac - bd
long ab_cd = karatsubaMultiply(max1 + min1, max2 + min2) - ac - bd;
// Combine the results
return (ac * (long) Math.pow(10, 2 * halfLength)) + (ab_cd * (long) Math.pow(10, halfLength)) + bd;
}
public static void main(String[] args) {
long num1 = 1234;
long num2 = 5678;
long result = karatsubaMultiply(num1, num2);
System.out.println("Product of " + num1 + " and " + num2 + " is " + result);
}
}
OutputProduct of 1234 and 5678 is 7006652
Similar Reads
Java Program to Implement the RSA Algorithm RSA or RivestâShamirâAdleman is an algorithm employed by modern computers to encrypt and decrypt messages. It is an asymmetric cryptographic algorithm. Asymmetric means that there are two different keys. This is also called public-key cryptography because one among the keys are often given to anyone
3 min read
Java Program to Implement the Schonhage-Strassen Algorithm for Multiplication of Two Numbers Schonhage-Strassen algorithm is one of the fastest ways of multiplying very large integer values (30000 to 150000 decimal digits). This algorithm was developed by Arnold Schönhage and Volker Strassen. Though the Furer's algorithm is faster than that of Schonhage-Strassen's algorithm, there are no pr
3 min read
Java Program to Print the Multiplication Table in a Triangle Form In this form, a table is displayed row and column-wise, in such a way such that in every row, only the entries up to the same column number filled. Example: Input : rows = 6 Output: 1 2 4 3 6 9 4 8 12 16 5 10 15 20 25 6 12 18 24 30 36 Approach: The idea is to use nested loops. First, display the col
2 min read
Java Program to Print Multiplication Table for Any Number Given a number n as input, we need to print its table, where N>0.Example:Input 1: N = 7Output: 7 * 1 = 7 7 * 2 = 14 7 * 3 = 21 7 * 4 = 28 7 * 5 = 35 7 * 6 = 42 7 * 7 = 49 7 * 8 = 56 7 * 9 = 63 7 * 10 = 70Ways to Print Multiplication Table for Any Number in JavaFour ways are shown to Print Multipl
4 min read
Implementing Strassenâs Algorithm in Java Strassen's algorithm is used for the multiplication of Square Matrices that is the order of matrices should be (N x N). Strassen's Algorithm is based on the divide and conquer technique. In simpler terms, it is used for matrix multiplication. Strassen's method of matrix multiplication is a typical d
8 min read
Java Program to Multiply two Matrices of any size In Java, Matrix Multiplication is a complex operation, unlike multiplying two constant numbers. In this article, we will learn How to multiply two matrices in Java.Example of Multiplication of Two Matrices Note: Two matrices are multiplicable if the number of coloumns in the first matrix is equal to
3 min read
How to Perform Java Parallel Matrix Multiplication? In Java, Parallelism can be used to perform Matrix Multiplication using multiple threads concurrently. The major advantage of this technique is that it uses distributed computing environments to speed up the computation. In this article, we will learn how to perform Parallel Matrix Multiplication in
4 min read
Java Program to Rotate digits of a given number by K Given two integers N and K, the task is to rotate the digits of N by K. If K is a positive integer, left rotate its digits. Otherwise, right rotate its digits. Examples: Input: N = 12345, K = 2Output: 34512 Explanation: Left rotating N(= 12345) by K(= 2) modifies N to 34512. Therefore, the required
2 min read
Implementing Coppersmith Winograd Algorithm in Java Coppersmith Winograd Algorithm is asymptotically the fastest known algorithm to date for the matrix multiplication algorithm. Algorithms with a preferred asymptotic running time over the Strassen calculation are rarely utilized practically, on the grounds that the huge steady factors in their runnin
3 min read
Java Program to Add Two numbers Without using Arithmetic Operator Here, we need to write a function that returns the sum of two stated integers. And the function must not utilize any of the arithmetic operators such as +, ++, â, -, .. Etc.). It will be a very basic elementary level program to compute the sum by simply using the '+' operator and thereby simply prin
2 min read