Pagini recente » Cod sursa (job #462899) | Cod sursa (job #2702334) | Cod sursa (job #1682380) | Cod sursa (job #1668856) | Cod sursa (job #2884259)
// C++ program to find modular inverse of a under modulo m
// This program works only if m is prime.
#include <iostream>
using namespace std;
// To find GCD of a and b
int gcd(int a, int b);
// To compute x raised to power y under modulo m
int power(int x, unsigned int y, unsigned int m);
// Function to find modular inverse of a under modulo m
// Assumption: m is prime
void modInverse(int a, int m)
{
int g = gcd(a, m);
if (g != 1)
cout << "Inverse doesn't exist";
else
{
// If a and m are relatively prime, then modulo
// inverse is a^(m-2) mode m
cout << "Modular multiplicative inverse is "
<< power(a, m - 2, m);
}
}
// To compute x^y under modulo m
int power(int x, unsigned int y, unsigned int m)
{
if (y == 0)
return 1;
int p = power(x, y / 2, m) % m;
p = (p * p) % m;
return (y % 2 == 0) ? p : (x * p) % m;
}
// Function to return gcd of a and b
int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
// Driver code
int main()
{
int a = 3, m = 11;
// Function call
modInverse(a, m);
return 0;
}