C Program to Implement the Schonhage-Strassen Algorithm for Multiplication

This is a C 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 source code of the C Program to Implement the Schonhage-Strassen Algorithm for Multiplication of Two Numbers. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. int noOfDigit(long a) {
  2.     int n = 0;
  3.     while (a > 0) {
  4.         a /= 10;
  5.         n++;
  6.     }
  7.     return n;
  8. }
  9. void schonhageStrassenMultiplication(long x, long y, int n, int m) {
  10.     int i, j;
  11.     int linearConvolution[n + m - 1];
  12.     for (i = 0; i < (n + m - 1); i++)
  13.         linearConvolution[i] = 0;
  14.     long p = x;
  15.     for (i = 0; i < m; i++) {
  16.         x = p;
  17.         for (j = 0; j < n; j++) {
  18.             linearConvolution[i + j] += (y % 10) * (x % 10);
  19.             x /= 10;
  20.         }
  21.         y /= 10;
  22.  
  23.     }
  24.     printf("The Linear Convolution is: ( ");
  25.     for (i = (n + m - 2); i >= 0; i--) {
  26.         printf("%d ", linearConvolution[i]);
  27.     }
  28.     printf(")");
  29.     long product = 0;
  30.     int nextCarry = 0, base = 1;
  31.     for (i = 0; i < n + m - 1; i++) {
  32.         linearConvolution[i] += nextCarry;
  33.         product = product + (base * (linearConvolution[i] % 10));
  34.         nextCarry = linearConvolution[i] / 10;
  35.         base *= 10;
  36.  
  37.     }
  38.     printf("The Product of the numbers is: %ld", product);
  39. }
  40. int main(int argc, char **argv) {
  41.     printf("Enter the numbers:");
  42.     long a, b;
  43.     scanf("%ld", &a);
  44.     scanf("%ld", &b);
  45.     int n = noOfDigit(a);
  46.     int m = noOfDigit(b);
  47.     schonhageStrassenMultiplication(a, b, n, m);
  48. }

Output:

$ gcc Schonhage-Strassen.c
$ ./a.out
 
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 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data Structures and Algorithms.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.