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;
}//Mainint 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.
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.
Related Posts:
- Apply for Computer Science Internship
- Practice Computer Science MCQs
- Check C++ Books
- Apply for C++ Internship
- Check Computer Science Books