C++ Program to Implement Euler Theorem

«
»
This C++ Program demonstrates the implementation of Euler Theorem. For the modular multiplicative inverse to exist, the number and modular must be coprime.

Here is source code of the C++ Program to implement Euler Theorem. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /* 
  2.  * C++ Program to Implement Euler Theorem
  3.  */
  4. #include <iostream>
  5. #include <vector>
  6. using namespace std;
  7.  
  8. vector<int> inverseArray(int n, int m) 
  9. {
  10.     vector<int> modInverse(n + 1, 0);
  11.     modInverse[1] = 1;
  12.     for (int i = 2; i <= n; i++) 
  13.     {
  14.         modInverse[i] = (-(m / i) * modInverse[m % i]) % m + m;
  15.     }
  16.     return modInverse;
  17. }
  18. //Main
  19. int main()
  20. {
  21.     vector<int>::iterator it;
  22.     int a, m;
  23.     cout<<"Enter number to find modular multiplicative inverse: ";
  24.     cin>>a;
  25.     cout<<"Enter Modular Value: ";
  26.     cin>>m;
  27.     cout<<inverseArray(a, m)[a]<<endl;
  28. }

advertisement
$ g++ euler.cpp
$ a.out
 
Enter number to find modular multiplicative inverse: 5
Enter Modular Value: 7
3
 
------------------
(program exited with code: 1)
Press return to continue

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn