- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program to Implement Euler Theorem
This is a C++ Program which demonstrates the implementation of Euler Theorem. The number and modular must be coprime for the modular multiplicative inverse to exist.
Algorithm
Begin Take input to find modular multiplicative inverse Take input as modular value Perform inverse array function: modInverse(x + 1, 0); modInverse[1] = 1; for i = 2 to x modInverse[i] = (-(y / i) * modInverse[y mod i]) mod y + y return modInverse End
Example Code
#include <iostream> #include <vector> using namespace std; vector<int> inverseArray(int x, int y) { vector<int> modInverse(x + 1, 0); modInverse[1] = 1; for (int i = 2; i <= x; i++) { modInverse[i] = (-(y / i) * modInverse[y % i]) % y + y; } return modInverse; } 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; }
Output
Enter number to find modular multiplicative inverse: 26 Enter Modular Value: 7 7
Advertisements