Tuesday, July 16, 2013

How to find Multiplicative Inverse using C++

#include <iostream>
#include <cstdio>
using namespace std;

int main()
{
    int q, r1, r2, r, t1, t2, t;
    while(cin>>r1>>r2){
        t1 = 0;
        t2 = 1;

        while(r2 != 0){
            q = r1 / r2;
            r = r1 % r2;
            t = t1 - t2*q;
            cout<<q<<" "<<r1<<" "<<r2<<" "<<r<<" "<<t1<<" "<<t2<<" "<<t<<endl;
            r1 = r2;
            r2 = r;
            t1 = t2;
            t2 = t;

        }

        if(r1 != 1) {
            puts("There is no multiplicative inverse");
        } else {
            cout<<t1<<endl;
        }
    }
    return 0;
}

No comments:

Post a Comment