Modular Inverse

Hello,

I'm trying to calculate the modular inverse of 4445 mod 7936. I wrote the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  #include <iostream>

using namespace std;

int n = 0;
int p = 4445*n;
int q = p%7936;

int main()
{
    while(q != 1)
    {
        n++;
        cout << q << endl;
    }
    return 0;
}


When my loop outputs 'q', it just keeps coming out to be 0. I've already found my answer to be 757, but I'd like to know what I did wrong for future reference (I'll be doing this calculation a number of times in the immediate future).

Any help is appreciated,

careyhrdng
On line 5, n = 0. So, on line 6, p = 4445 * 0 = 0 and on line 7, q = 0 %7936 = 0. Thats why no matter what is the value of n, q is always 0. So, you need to change the value of p and q in the while loop..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

using namespace std;

int n = 0;
int p;
int q;

int main()
{
    while(q != 1)
    {
        p = 4445 * n;
        q = p%7936;
        n++;
        cout << q << endl;
    }
    return 0;
}

Thank's a bunch!
Topic archived. No new replies allowed.