Vous êtes sur la page 1sur 4

Lecture 9, 09-14-11

CS 2050, Intro Discrete Math for Computer Science

Multiplicative Inverses via Euclids Algorithm


Suppose that we want to find the multiplicative inverse of 79 in arithmetic modulo 1249 (where
1249 is a prime number, hence this multiplicative inverse exists and it is unique). So we are looking
for z such that
z 79 1 mod 1249 .
The approach is to use quantities involved in the computation of gcd(1249,79)=1 and write 1 as
a linear combination of 79 and 1249. The multiplicative inverse of 79 follows immediately, by
considering all quantities mod 1249. In particular, we will see below that we can write:
1 = 332 79 + 21 1249 .
This immediately implies
1 332 79 mod 1249 .
Equivalently, since
332 (1249332) mod 1249 ,
or
332 917 mod 1249
we get
1 917 79 mod 1249 .
Thus the multiplicative inverse of 79 in arithmetic modulo 1249 is 917.

Of course, the key was to write 1 = 332 79 + 21 1249.


How did we find the numbers -332 and 21?

We found the numbers -332 and 21 in the equation


1 = 332 79 + 21 1249
by working bottom-up with dividors, quotients and remainders
appearing in Euclids algorithm, while computing gcd(1249,79):
1249
79
64
15
4
3

1 =
1 =
1 =
1 =
1 =
1 =
1 =
1 =
1 =
1 =
1 =
1 =
1 =

1 3

=
=
=
=
=
=

quotient
15
1
4
3
1
3

dividor
79
64
15
4
3
1

+
+
+
+
+
+

remainder
64
15
4
3
1
0

(Line
(Line
(Line
(Line
(Line

1)
2)
3)
4)
5)

from Line 5: 3 is the dividor and 4 is left-hand-side


in Line 4: 3 is the remainder and 4 is the dividor
1 (3 4 + 15)
+
4
from Line 4: substitute the remainder 3
((1) (3) + 1) 4
+ (1) 15
rearrange so as to keep only 4 and 15 because:
44
+ (1) 15 in Line 4: 4 is the dividor and 15 is the left-hand-side
in Line 3: 4 is the remainder and 15 is the dividor
4 (4 15 + 64)
+ (1) 15
from Line 3: substitute the remainder 4
(4 (4) + (1)) 15
+
4 64
rearrange so as to keep only 15 and 64 because:
(17) 15
+
4 64
in Line 3: 15 is the dividor and 64 is the left-hand-side
in Line 2: 15 is the remainder and 64 is the dividor
17 (1 64 + 79)
+
4 64
from Line 2: substitute the remainder 15
((17) (1) + 4) 64 + (17) 79
rearrange so as to keep only 64 and 79 because:
21 64
+ (17) 79 in Line 2: 64 is the dividor and 79 is the left-hand-side
in Line 1: 64 is the remainder and 79 is the dividor
21 (15 79 + 1249) + (17) 79
from Line 1: substitute 64
(21 (15) + (17)) 79 + 21 1249
rearrange so as to keep only 79 and 1249 because
332 79
+ 21 1249
this was our original goal

Let us re-examine Euclids algorithm:


procedure gcd(a, b: positive integers);
x1 := a ;
y1 := b ;
q1 := x1 div y1 ;
r1 := x1 mod y1 ;
k := 1 ;
while rk 6= 0
begin
k := k + 1 ;
xk := yk1 ;
Remark: Realize that, for all i, this assignment implies:
yk := rk1 ;
yi = ri1 and xi = ri2 .
qk := xk div yk ;
rk := xk mod yk ;
end;
return(yk );
%comment: if k > 1 then yk = rk1 ;
Therefore, in execution we will get:
x1
y1
r1
r2
r3
rk4
rk3
rk2

q1 y 1
q 2 r1
q 3 r2
q 4 r3
q 5 r4
...
= qk2 rk3
= qk1 rk2
= qk rk1
=
=
=
=
=

+
+
+
+
+

r1
r2
r3
r4
r5

Remark: Realize that, for all i, we have


ri = qi ri1 + ri2 .

+ rk2
+ rk1 Remark: rk1 = gcd(x1 , y1 ) = gcd(a, b)
+ 0

L
Thus, at the (k1)st line, for Q
k1 = qk1 and k1 = 1 we have

rk1 = gcd(a, b) = qk1 rk2 +


rk3
Q
L
= k1 rk2 + k1 rk3
L
Now supposing that, at the i-th line, for some Q
i and i we could write
L
gcd(a, b) = Q
i ri1 + i ri2

and recalling that, for all i, ri = qi ri1 + ri2 , thus also ri1 = qi1 ri2 + ri3 we get
L
gcd(a, b) = Q
i ri1 + i ri2
= Q
(qi1 ri2 + ri3 ) + Li ri2
i

L
= Q

q
+

ri2 + Q
i1
i
i
i ri3

Q
Q
L
L
which implies that, at the (i1)-st line, for Q
i1 = (i qi1 + i ) and i1 = i we can write
L
gcd(a, b) = Q
i1 ri2 + i1 ri3 .

procedure mult-inverse(a, b : positive integers);


q1 := a div b ;
r1 := a mod b ;
k := 1 ; q0 := a ; r0 := b ;
while rk 6= 0
begin
k := k + 1 ;
qk := rk2 div rk1 ;
rk := rk2 mod rk1 ;
end; %comment: gcd(a, b) = rk1 ;
L
while i > 1
Q
k1 := qk1 ; k1 := 1 ; i := k1 ;
begin
Q
L
Q
i1 := i qi1 + i ;
Li1 := Q
i ;
i := i1 ;
end;
if gcd(a, b) = 1 then return(Q
1 mod a) else return(error: gcd(a, b) 6= 1);
On input 1249, 79, the above algorithm computes:
k = 1, q0 = 1249, r0 = 79, q1 = 15, r1 = 64,
k = 2, q2 = 1 r2 = 15,
k = 3, q3 = 4 r3 = 4,
k = 4, q4 = 3 r4 = 3,
k = 5, q5 = 1 r5 = 1,
k = 6, q6 = 3 r6 = 0, hence gcd(1249, 79) = 1,
L
i = 5, Q
5 = (1), 5 = 1,
Q
i = 4, 4 = 4, L4 = (1),
L
i = 3, Q
3 = (17), 3 = 4,
L
i = 2, Q
2 = 21, 5 = (17),
Q
i = 1, 1 = (332), L1 = 21,
return((-332) mod 1429)
return(917)

Vous aimerez peut-être aussi