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.

1. //This is a java program to find the multiplication of the two numbers using Schonhage-Strassen Algorithm
2. import java.util.Scanner;
3.
4. public class Schonhage_Strassen_Algorithm
5. {
6.     public static int noOfDigit(long a)
7.     {
8.         int n=0;
9.         while(a>0)
10.         {
11.             a /= 10;
12.             n++;
13.         }
14.         return n;
15.     }
16.     public static void schonhageStrassenMultiplication(long x, long y, int n, int m)
17.     {
18.
19.         int []linearConvolution = new int[n+m-1];
20.         for(int i=0; i<(n+m-1); i++)
21.             linearConvolution[i] = 0;
22.
23.         long p=x;
24.         for(int i=0; i<m; i++)
25.         {
26.             x = p;
27.             for(int j=0; j<n; j++)
28.             {
29.                 linearConvolution[i+j] += (y%10) * (x%10);
30.                 x /= 10;
31.             }
32.             y /= 10;
33.         }
34.         System.out.print("The Linear Convolution is: ( ");
35.         for(int i=(n+m-2); i>=0; i--)
36.         {
37.             System.out.print(linearConvolution[i] +" ");
38.         }
39.         System.out.println(")");
40.
41.         long product = 0;
42.         int nextCarry=0, base=1;;
43.         for(int i=0; i<n+m-1; i++)
44.         {
45.             linearConvolution[i] += nextCarry;
46.             product = product + (base * (linearConvolution[i]%10));
47.             nextCarry = linearConvolution[i]/10;
48.             base *= 10;
49.         }
50.         System.out.println("The Product of the numbers is: " + product);
51.
52.     }
53.     public static void main(String args[])
54.     {
55.         Scanner input = new Scanner(System.in);
56.         System.out.println("Enter the numbers:");
57.         long a = input.nextLong();
58.         long b = input.nextLong();
59.         int n = noOfDigit(a);
60.         int m = noOfDigit(b);
61.         schonhageStrassenMultiplication(a, b, n, m);
62.
63.         input.close();
64.     }
65. }

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

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now! 