Open In App

Java Program to Implement the Karatsuba Multiplication Algorithm

Last Updated : 14 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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 :

  1. Third-grade school algorithm Method (Standard-way)
  2. Recursive algorithm Method
  3. 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:

  1. If x < 10 or y < 10, return x * y.
  2. Compute halfLength = max(length(x), length(y)) / 2.
  3. Split x into max1 = x / 10^halfLength and min1 = x % 10^halfLength.
  4. Split y into max2 = y / 10^halfLength and min2 = y % 10^halfLength.
  5. Compute ac = karatsubaMultiply(max1, max2).
  6. Compute bd = karatsubaMultiply(min1, min2).
  7. Compute ab_cd = karatsubaMultiply(max1 + min1, max2 + min2).
  8. 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
Illustration of the Steps
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);
    }
}

Output
Product of 1234 and 5678 is 7006652


Next Article

Similar Reads