Java Program to Implement the Schonhage-Strassen Algorithm for Multiplication of Two Numbers

This is a sample program to multiply two given numbers using Schonhage-Strassen Algorithm. Suppose we are multiplying two numbers like 123 and 456 using long multiplication with base B digits, but without performing any carrying. The result might look something like this:
0 1 2 3
× 4 5 6
———————
00 00 06 12 18
00 05 10 15 00
04 08 12 00 00
———————
04 13 28 27 18
This sequence (4, 13, 28, 27, 18) is called the acyclic or linear convolution of the two original sequences (1,2,3) and (4,5,6). Once you have the acyclic convolution of two sequences, computing the product of the original numbers is easy: you just perform the carrying (for example, in the rightmost column, you’d keep the 8 and add the 1 to the column containing 27). In the example this yields the correct product 56088.

Here is the source code of the Java Program to Implement the Schonhage-Strassen Algorithm for Multiplication of Two Numbers. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

//This is a java program to find the multiplication of the two numbers using Schonhage-Strassen Algorithm
import java.util.Scanner;
 
public class Schonhage_Strassen_Algorithm 
{
    public static int noOfDigit(long a)
    {
        int n=0;
        while(a>0)
        {
            a /= 10; 
            n++;
        }
        return n;
    }
    public static void schonhageStrassenMultiplication(long x, long y, int n, int m)
    {
 
        int []linearConvolution = new int[n+m-1];
        for(int i=0; i<(n+m-1); i++)
            linearConvolution[i] = 0;
 
        long p=x;
        for(int i=0; i<m; i++)
        {
            x = p;
            for(int j=0; j<n; j++)
            {
                linearConvolution[i+j] += (y%10) * (x%10);
                x /= 10;
            }
            y /= 10;
        }
        System.out.print("The Linear Convolution is: ( ");
        for(int i=(n+m-2); i>=0; i--)
        {
            System.out.print(linearConvolution[i] +" ");
        }
        System.out.println(")");
 
        long product = 0;
        int nextCarry=0, base=1;;
        for(int i=0; i<n+m-1; i++)
        {
            linearConvolution[i] += nextCarry;
            product = product + (base * (linearConvolution[i]%10));
            nextCarry = linearConvolution[i]/10;
            base *= 10;
        }
        System.out.println("The Product of the numbers is: " + product);
 
    }
    public static void main(String args[])
    {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter the numbers:");
        long a = input.nextLong();
        long b = input.nextLong();
        int n = noOfDigit(a);
        int m = noOfDigit(b);
        schonhageStrassenMultiplication(a, b, n, m);
 
        input.close();
    }
}

Output:

$ javac Schonhage_Strassen_Algorithm.java
$ java Schonhage_Strassen_Algorithm
 
Enter the numbers:
456
123
The Linear Convolution is: ( 4 13 28 27 18 )
The Product of the numbers is: 56088

Related posts:

Tạo chương trình Java đầu tiên sử dụng Eclipse
Lập trình hướng đối tượng (OOPs) trong java
Predicate trong Java 8
Spring Security 5 for Reactive Applications
Java 8 – Powerful Comparison with Lambdas
Spring Boot - Google Cloud Platform
Java Perform to a 2D FFT Inplace Given a Complex 2D Array
Limiting Query Results with JPA and Spring Data JPA
Spring Boot - Quick Start
Java Program to Implement Fermat Primality Test Algorithm
Java Program to Perform the Shaker Sort
Java Program to Implement the Monoalphabetic Cypher
Hướng dẫn sử dụng Printing Service trong Java
Java Program to Implement TreeMap API
Java Program to Implement Dijkstra’s Algorithm using Queue
Java Program to Check whether Directed Graph is Connected using BFS
Tránh lỗi ConcurrentModificationException trong Java như thế nào?
Generating Random Numbers in a Range in Java
Jackson – Bidirectional Relationships
Vấn đề Nhà sản xuất (Producer) – Người tiêu dùng (Consumer) và đồng bộ hóa các luồng trong Java
Java Program to implement Array Deque
Checking for Empty or Blank Strings in Java
A Guide to ConcurrentMap
Java Program to Implement Best-First Search
Spring Security Logout
Giới thiệu Java Service Provider Interface (SPI) – Tạo các ứng dụng Java dễ mở rộng
Lập trình đa luồng trong Java (Java Multi-threading)
Java Program to Perform Complex Number Multiplication
Java Program to Implement Bloom Filter
Java Program to Find kth Largest Element in a Sequence
Running Spring Boot Applications With Minikube
Java Program to Implement Segment Tree