Java Program to Implement Karatsuba Multiplication Algorithm

This is a Java Program to Implement Karatsuba Multiplication Algorithm. The Karatsuba algorithm is a fast multiplication algorithm. It was discovered by Anatolii Alexeevitch Karatsuba in 1960 and published in 1962. The Karatsuba algorithm was the first multiplication algorithm asymptotically faster than the quadratic “grade school” algorithm. It reduces the multiplication of two n-digit numbers to at most 3 n log approx 3 n 1.585 single-digit multiplications in general.

Here is the source code of the Java Program to Implement Karatsuba Multiplication Algorithm. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

/**
 *    Java Program to Implement Karatsuba Multiplication Algorithm
 **/
 
import java.util.Scanner;
 
/** Class Karatsuba **/
public class Karatsuba
{
    /** Function to multiply two numbers **/
    public long multiply(long x, long y)
    {
        int size1 = getSize(x);
        int size2 = getSize(y);
        /** Maximum of lengths of number **/
        int N = Math.max(size1, size2);
        /** for small values directly multiply **/        
        if (N < 10)
            return x * y;
        /** max length divided, rounded up **/    
        N = (N / 2) + (N % 2);    
        /** multiplier **/
        long m = (long)Math.pow(10, N);
 
        /** compute sub expressions **/        
        long b = x / m;
        long a = x - (b * m);
        long d = y / m;
        long c = y - (d * N);
        /** compute sub expressions **/
        long z0 = multiply(a, c);
        long z1 = multiply(a + b, c + d);
        long z2 = multiply(b, d);          
 
        return z0 + ((z1 - z0 - z2) * m) + (z2 * (long)(Math.pow(10, 2 * N)));        
    }
    /** Function to calculate length or number of digits in a number **/
    public int getSize(long num)
    {
        int ctr = 0;
        while (num != 0)
        {
            ctr++;
            num /= 10;
        }
        return ctr;
    }
    /** Main function **/
    public static void main (String[] args) 
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("Karatsuba Multiplication Algorithm Test\n");
        /** Make an object of Karatsuba class **/
        Karatsuba kts = new Karatsuba();
 
        /** Accept two integers **/
        System.out.println("Enter two integer numbers\n");
        long n1 = scan.nextLong();
        long n2 = scan.nextLong();
        /** Call function multiply of class Karatsuba **/
        long product = kts.multiply(n1, n2);
        System.out.println("\nProduct : "+ product);
    }
}

Output:

Karatsuba Multiplication Algorithm Test
 
Enter two integer numbers
 
24061994 28563
 
Product : 687282734622

Related posts:

Java Program to Check whether Graph is a Bipartite using DFS
A Quick JUnit vs TestNG Comparison
Java Program to Implement Hopcroft Algorithm
Java Program to Find the Longest Path in a DAG
Java Program to Implement CopyOnWriteArraySet API
Netflix Archaius with Various Database Configurations
Java Program to Find Whether a Path Exists Between 2 Given Nodes
Java Program to Check if a Given Binary Tree is an AVL Tree or Not
Java Program to Implement AttributeList API
Java Program to Implement Bellman-Ford Algorithm
Java Program to Perform Preorder Recursive Traversal of a Given Binary Tree
Java Program to Solve Travelling Salesman Problem for Unweighted Graph
Java Program to Implement the One Time Pad Algorithm
Spring – Injecting Collections
Java Program to Give an Implementation of the Traditional Chinese Postman Problem
Java Program to Implement Suffix Tree
Jackson Ignore Properties on Marshalling
Java Program to Find Location of a Point Placed in Three Dimensions Using K-D Trees
Java Program to Implement Hash Tables with Quadratic Probing
Programmatic Transaction Management in Spring
Java Program to Implement Solovay Strassen Primality Test Algorithm
DynamoDB in a Spring Boot Application Using Spring Data
Java Program to Implement PriorityBlockingQueue API
Làm thế nào tạo instance của một class mà không gọi từ khóa new?
How to use the Spring FactoryBean?
Java Program for Topological Sorting in Graphs
A Custom Data Binder in Spring MVC
Mệnh đề if-else trong java
Toán tử instanceof trong java
The Dining Philosophers Problem in Java
Java Deep Learning Essentials - Yusuke Sugomori
Count Occurrences of a Char in a String