Computer >> कंप्यूटर >  >> प्रोग्रामिंग >> C++

दो संख्याओं के गुणन के लिए शॉनहेज-स्ट्रैसन एल्गोरिथम को लागू करने के लिए C++ कार्यक्रम

Schonhage-Strassen Algorithm का उपयोग दो संख्याओं को गुणा करने के लिए किया जाता है। SchonhageStrassen एल्गोरिथ्म बड़े पूर्णांकों के लिए एक स्पर्शोन्मुख रूप से तेज़ गुणन एल्गोरिथ्म है। व्यवहार में शॉनहेज-स्ट्रैसन एल्गोरिथम 2 2 से आगे की संख्या के लिए करात्सुबा और टूम-कूक जैसे पुराने तरीकों से बेहतर प्रदर्शन करना शुरू कर देता है 15 2 217 . तक (10,000 से 40,000 दशमलव) अंक।

एल्गोरिदम

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

उदाहरण कोड

#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);
}

आउटपुट

Enter the numbers:1234 5679
The Linear Convolution is: ( 5 16 34 61 63 55 36 )
The Product of the numbers is: 7007886

  1. सी ++ प्रोग्राम विगेनियर साइफर को लागू करने के लिए

    Vigenere Cipher, अल्फ़ाबेटिक टेक्स्ट को एन्क्रिप्ट करने की एक तरह की पॉलीअल्फ़ाबेटिक प्रतिस्थापन विधि है। इस विधि में एन्क्रिप्शन और डिक्रिप्शन के लिए Vigenere Cipher Table का उपयोग किया जाता है जिसमें A से Z तक के अक्षर 26 पंक्तियों में लिखे जाते हैं। एन्क्रिप्शन कुंजी: स्वागत है संदेश: यहिस्ट

  1. C++ प्रोग्राम रिकर्सिव यूक्लिड एल्गोरिथम का उपयोग करके दो नंबरों की GCD खोजने के लिए

    दो संख्याओं का सबसे बड़ा सामान्य भाजक (GCD) उन दोनों को विभाजित करने वाली सबसे बड़ी संख्या है। उदाहरण के लिए:मान लें कि हमारे पास दो संख्याएँ हैं जो 63 और 21 हैं। 63 = 7 * 3 * 3 21 = 7 * 3 तो, 63 और 21 का जीसीडी 21 है। पुनरावर्ती यूक्लिड का एल्गोरिथ्म सकारात्मक पूर्णांक a और b की एक जोड़ी का उपयो

  1. C++ प्रोग्राम दो नंबर स्वैप करने के लिए

    दो नंबरों को स्वैप करने के लिए प्रोग्राम बनाने के दो तरीके हैं। एक में एक अस्थायी चर का उपयोग करना शामिल है और दूसरा तरीका तीसरे चर का उपयोग नहीं करता है। इन्हें विस्तार से इस प्रकार समझाया गया है - अस्थायी चर का उपयोग करके दो नंबरों को स्वैप करने का कार्यक्रम एक अस्थायी चर का उपयोग करके दो नंबरों