
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Implement the Schonhage-Strassen Algorithm for Multiplication of Two Numbers
Schonhage-Strassen Algorithm is used to multiply two numbers. The SchonhageStrassen algorithm is an asymptotically fast multiplication algorithm for large integers. In practice the Schonhage-Strassen algorithm starts to outperform older methods like karatsuba and Toom-CooK for numbers beyond 2215 to 2217 (10,000 to 40,000 decimal) digits.
Algorithm
Begin function noOfDigit( x) Declare a variable n and assign n = 0; while (x > 0) x = x /10 Increment n return n End Begin Algorithm for schonhageStrassenMultiplication: schonhageStrassenMultiplication(a, b, n, m) define an array linearConvolution[n + m - 1] for i = 0 to (n + m - 1)-1 linearConvolution[i] = 0; long p = a for i = 0 to m-1 a = p for j = 0 to n-1 linearConvolution[i + j] += (b mod 10) * (a mod 10); a /= 10 b /= 10 for i = (n + m - 2) to 0 Print linearConvolution[i] long product = 0 nextCarry = 0, base = 1 for i = 0 to (n + m - 1)-1 linearConvolution[i] += nextCarry; product = product + (base * (linearConvolution[i] % 10)); nextCarry = linearConvolution[i] / 10; base *= 10; Print the product of numbers. End
Example Code
#include <iostream> using namespace std; int noOfDigit(long x) { int n = 0; while (x > 0) { x /= 10; n++; } return n; } void schonhageStrassenMultiplication(long a, long b, int n, int m) { int linearConvolution[n + m - 1]; for (int i = 0; i < (n + m - 1); i++) linearConvolution[i] = 0; long p = a; for (int i = 0; i < m; i++) { a = p; for (int j = 0; j < n; j++) { linearConvolution[i + j] += (b % 10) * (a % 10); a /= 10; } b /= 10; } cout << "The Linear Convolution is: ( "; for (int i = (n + m - 2); i >= 0; i--) { cout << linearConvolution[i] << " "; } cout << ")"; 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; } cout << "\nThe Product of the numbers is: " << product; } int main(int argc, char **argv) { cout << "Enter the numbers:"; long a, b; cin >> a >> b; int n = noOfDigit(a); int m = noOfDigit(b); schonhageStrassenMultiplication(a, b, n, m); }
Output
Enter the numbers:1234 5679 The Linear Convolution is: ( 5 16 34 61 63 55 36 ) The Product of the numbers is: 7007886
- Related Articles
- C++ Program to Implement Booth’s Multiplication Algorithm for Multiplication of 2 signed Numbers
- C++ Program to Implement Bitap Algorithm for String Matching
- C++ Program to implement Gift Wrapping Algorithm in Two Dimensions
- C++ Program to Implement the RSA Algorithm
- C++ Program to Implement Russian Peasant Multiplication
- C++ Program to Implement Kadane’s Algorithm
- C++ Program to Implement Johnson’s Algorithm
- C++ Program to Implement Fisher-Yates Algorithm for Array Shuffling
- C++ Program to Implement The Edmonds-Karp Algorithm
- C++ Program to Implement the Bin Packing Algorithm
- C++ Program to Implement the String Search Algorithm for Short Text Sizes
- C++ Program to Implement Nearest Neighbour Algorithm
- C++ Program to Implement Expression Tree Algorithm
- C++ Program to Implement Interpolation Search Algorithm
- C++ Program to Implement Extended Euclidean Algorithm

Advertisements