यह मोड्यूलर एक्सपोनेंशिएशन एल्गोरिदम लागू करने के लिए एक C++ प्रोग्राम है।
एल्गोरिदम
Begin
function modular():
// आर्गुमेंट्स: base, exp, mod.
// फंक्शन बॉडी:
res = 1 इनिशियलाइज़ करें
while (exp > 0)
if (exp % 2 == 1)
res = (res * base) % mod
exp = exp >> 1
base = (base * base) % mod
return res.
End
C++ कार्यान्वयन
#include <iostream>
using namespace std;
long long modular(long long base, long long exp, int mod) {
long long res = 1;
while (exp > 0) {
if (exp % 2 == 1)
res = (res * base) % mod;
exp = exp >> 1;
base = (base * base) % mod;
}
return res;
}
int main() {
long long b, e;
int mod;
cout << "Enter Base: ";
cin >> b;
cout << "Enter Exponent: ";
cin >> e;
cout << "Enter Modular Value: ";
cin >> mod;
cout << modular(b, e, mod);
return 0;
}
आउटपुट
Enter Base: 7 Enter Exponent: 6 Enter Modular Value: 26 25
व्याख्या
7⁶ mod 26 = 117649 mod 26 = 25। यह एल्गोरिदम O(log n) समय में काम करता है बाइनरी एक्सपोनेंशिएशन का उपयोग करके।
आवेदन
- क्रिप्टोग्राफी (RSA, Diffie-Hellman)
- बड़े नंबरों पर कम्प्यूटेशनल मैथमेटिक्स
- कॉम्पिटिटिव प्रोग्रामिंग में मॉड्यूलर अर्थमेटिक