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.
/*
* C++ Program to Implement Euler Theorem
*/
#include <iostream>
#include <vector>
using namespace std;
vector<int> inverseArray(int n, int m)
{
vector<int> modInverse(n + 1, 0);
modInverse[1] = 1;
for (int i = 2; i <= n; i++)
{
modInverse[i] = (-(m / i) * modInverse[m % i]) % m + m;
}
return modInverse;
}
//Main
int main()
{
vector<int>::iterator it;
int a, m;
cout<<"Enter number to find modular multiplicative inverse: ";
cin>>a;
cout<<"Enter Modular Value: ";
cin>>m;
cout<<inverseArray(a, m)[a]<<endl;
}
$ 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.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.
Next Steps:
- Get Free Certificate of Merit in C++ Programming
- Participate in C++ Programming Certification Contest
- Become a Top Ranker in C++ Programming
- Take C++ Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Related Posts:
- Buy Programming Books
- Practice Programming MCQs
- Buy Computer Science Books
- Practice Computer Science MCQs
- Apply for C++ Internship