Skip to content
Related Articles

Related Articles

Java Program to Implement the Karatsuba Multiplication Algorithm
  • Last Updated : 26 Nov, 2020

It is a replacement for the grade school algorithm for multiplying numbers. of bigger digits. The Karatsuba Algorithm for fast multiplication algorithm using Divide and Conquer developed by Anatolii Alexeevitch Karatsuba in 1960. The first thing that hits the head what is it and why it is designed. Though 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 designed because the design space is surprisingly rich. Its time complexity is:

 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.



The algorithm is standardized for 4 digits for sake of understanding. One can multiply as many digits taken into sets.

Algorithm Steps:

  1. Compute starting set  (a*c)
  2. Compute set after starting set may it be ending set (b*d)
  3. Compute starting set with ending sets
  4. Subtract values of step 3 from step2 from step1
  5. Pad up (Add) 4 zeros to the number obtained from Step1, step2 value unchanged, and pad up two zeros to vale obtained from step4.

An example illustrating the Karatsuba Algorithm:

Input: x = 1234, y = 5678

Processing: As per above inputs

x = 1234

y = 5678



a = 12, b = 34

c = 56, d = 78

Step 1

a * c = 172     

Step 2            

b * d = 2652          

Step 3

(a+b)(c+d) = 134 * 36 = 6164

Step 4:

6164 – 2652 – 172 = 2840

Step 5:

1720000 + 2652 + 284000 = 7006652

Output: 7006652

The value obtained from step 5 is in fact the product obtained if standard school multiplication is carried on between these two numbers ‘x’ and ‘y’.

1720000 + 2652 + 284000 = 7006652

Implementation: Note not to grasp the formulas laid down for this algorithm rather understanding it in this way makes its way to better.

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate
// karatsuba multiplication
  
// Importing generic java libraries
import java.io.*;
  
class GFG {
  
    // Karatsuba multiplication class
    public static long karatmultiply(long x, long y)
    {
        // Finding length of both integer
        // numbers x and y
        int noOneLength = numLength(x);
        int noTwoLength = numLength(y);
  
        // Find maximum length from both numbers
        // using math library max function
        int maxNumLength
            = Math.max(noOneLength, noTwoLength);
  
        // For very small values
        // multiply directly
        int smallValue = 1 << 4;
        if (maxNumLength < smallValue)
            return x * y;
  
        // Rounding up the
        // divided Max length
        maxNumLength
            = (maxNumLength / 2) + (maxNumLength % 2);
  
        // Multiplier
        long maxNumLengthTen
            = (long)Math.pow(10, maxNumLength);
  
        // Compute the expressions
        long b = x / maxNumLengthTen;
        long a = x - (b * maxNumLengthTen);
        long d = y / maxNumLengthTen;
        long c = y - (d * maxNumLength);
  
        // Compute all mutilpying variables
        // needed to get the multiplication
        long z0 = karatmultiply(a, c);
        long z1 = karatmultiply(a + b, c + d);
        long z2 = karatmultiply(b, d);
  
        return z0 + ((z1 - z0 - z2) * maxNumLengthTen)
            + (z2 * (long)(Math.pow(10, 2 * maxNumLength)));
    }
  
    // Fucntion to calculate
    // length of the number
    public static int numLength(long n)
    {
        int noLen = 0;
        while (n > 0) {
            noLen++;
            n /= 10;
        }
  
        // Returning length of number n
        return noLen;
    }
  
    // Main driver function
    public static void main(String[] args)
    {
        // Showcasing karatsuba multiplication
        // case 1: Big integer lengths
        long product1 = karatmultiply(1345, 63456);
        System.out.println("Product 1 : " + product1);
  
        // Case 2: quite smaller integer length
        long product2 = karatmultiply(102, 213);
        System.out.println("Product 2 : " + product2);
    }
}

chevron_right


Output

Product 1 : 85348320
Product 2 : 21726

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :