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

advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification 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
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 20s–40s and exploring new directions in your career, I also offer mentoring. Learn more here.