Question

In: Computer Science

Write a program( preferably in C++)  using the extended Euclidean algorithm to find the multiplicative inverse of...

Write a program( preferably in C++)  using the extended Euclidean algorithm to find the multiplicative inverse of a mod n. Your program should allow user to enter a and n.

Note: For this question please make sure the code compiles and runs, it is not copied and pasted from elsewhere( I will be checking!). Thanks

Solutions

Expert Solution

C++ code to find the multiplicative inverse of a mod n:

#include<iostream> 
using namespace std; 

int extended_Euclidean_GCD(int a, int b, int *x, int *y); 

void multiplicative_Inverse(int a, int n) 
{ 
        int x, y; 
        int g = extended_Euclidean_GCD(a, n, &x, &y);
        
        //if not co-primes then inverse doesn't exist 
        if (g != 1)                     
                cout << "Inverse doesn't exist"; 
        else
        { 
                int mul_inv = (x%n + n) % n;        //Adding n to avoid negative x cases
                cout << "The multiplicative inverse is: " << mul_inv; 
        } 
} 

//Recursive function to implement extended euclidean algorithm
int extended_Euclidean_GCD(int a, int b, int *x, int *y) 
{ 
        if (a == 0) 
        { 
                *x = 0, *y = 1; 
                return b; 
        } 

        int X_1, Y_1; 
        int GCD = extended_Euclidean_GCD(b%a, a, &X_1, &Y_1); 
        *x = Y_1 - (b/a) * X_1; 
        *y = X_1; 
        return GCD; 
} 


//Driver main()
int main() 
{ 
        int A,N;
        cout<<"Enter the value of a: ";
        cin>>A;
        cout<<"Enter the value of n: ";
        cin>>N; 
        multiplicative_Inverse(A, N); 
        return 0; 
} 

OUTPUT:


Related Solutions

find a linear combination for gcd(259,313). use extended euclidean algorithm. what is inverse of 259 in...
find a linear combination for gcd(259,313). use extended euclidean algorithm. what is inverse of 259 in z subscript 313? what is inverse of 313 in z subscript 259?
a. Using the Euclidean Algorithm and Extended Euclidean Algorithm, show that gcd(99; 5) = 1 and...
a. Using the Euclidean Algorithm and Extended Euclidean Algorithm, show that gcd(99; 5) = 1 and find integers s1 and t1 such that 5s1 + 99t1 = 1. [Hint: You should find that 5(20) + 99(?1) = 1] b. Solve the congruence 5x 17 (mod 99) c. Using the Chinese Remainder Theorem, solve the congruence x 3 (mod 5) x 42 (mod 99) d. Using the Chinese Remainder Theorem, solve the congruence x 3 (mod 5) x 6 (mod 9)...
1. Show that 11,111,111 and 3,333,333 are relatively prime using the Extended Euclidean Algorithm.
  1. Show that 11,111,111 and 3,333,333 are relatively prime using the Extended Euclidean Algorithm. 2. Use the EEA to find the GCD of 6,327 and 10,101. 3. Find the additive inverse of 3,333,333 modulo 11,111,111. Verify. 4. Find the multiplicative inverse of 3,333,333 modulo 11,111,111.Verify. 5. What is the orbit of 3 in the group Z_7 under multiplication modulo 7? Is 3 a generator?
Using Euclidean algorithm, Find integers x and y with 65537x + 3511y = 17.
Using Euclidean algorithm, Find integers x and y with 65537x + 3511y = 17.
Suppose you Do not know anything about Extended Euclidean Algorithm. How to find t(x) and s(x)...
Suppose you Do not know anything about Extended Euclidean Algorithm. How to find t(x) and s(x) that satisfy the greatese common divisor of f(x) and g(x) equals to f(x)t(x)+g(x)s(x) in Q(x). You can give me an example(polynomials) if you want. Thank you!
Using extended euclidean algorithm find f(x) and g(x) in: f(x)(x^5 + 4x^4 + 6x^3 + x^2...
Using extended euclidean algorithm find f(x) and g(x) in: f(x)(x^5 + 4x^4 + 6x^3 + x^2 + 4x + 6) + g(x)(x^5 + 5x^4 + 10x^3 + x^2 + 5x + 10) = x^3+1
1. Find the multiplicative inverse of 14 in GF(31) domain using Fermat’s little theorem. Show your...
1. Find the multiplicative inverse of 14 in GF(31) domain using Fermat’s little theorem. Show your work. 2 Using Euler’s theorem to find the following exponential: 4200 mod 27. Show how you have employed Euler’s theorem here.
Q4. Find the multiplicative inverse of 14 in GF(31) domain using Fermat’s little theorem. Show your...
Q4. Find the multiplicative inverse of 14 in GF(31) domain using Fermat’s little theorem. Show your work Q5. Using Euler’s theorem to find the following exponential: 4200mod 27. Show how you have employed Euler’s theorem here
write an algorithm program using python or C++ where a0=1, a1=2 an=an-1*an-2, find an ,also a5=?
write an algorithm program using python or C++ where a0=1, a1=2 an=an-1*an-2, find an ,also a5=?
write C++ program using functions (separate function for each bottom) Write a program to find if...
write C++ program using functions (separate function for each bottom) Write a program to find if a number is large word for two given bottom base - bottom1 and bottom2. You can predict that a number, when converted to any given base shall not exceed 10 digits. . the program should ask from user to enter a number that it should ask to enter the base ranging from 2 to 16 after that it should check if the number is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT