Euler Theorem in C++

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. }

$ 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
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

If you find any mistake above, kindly email to [email protected]

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.