Division Algorithm
Topics
GCD
EGCD
Multiplicative inverse under mod m
Given a number a (say 13) and divisor d (say 5), then there exist q
(=2) and r (=3)
such that a = (d )q+ (r)
Example : 13= (5)x2 +(3).
Proposition
if a=d.q+r, the greatest common divisor of (a,d) pair is same as that of
(d,r) pair, where a,d,q,r are integers
gcd(big,small)=gcd(small,remainder)
It basically follows from
For a , b not both zero
a) gcd(a,b)=gcd(b,a)
b) if a>0 and a|b then gcd(a,b)=a
c) then
1
Euclid's Algorithm with an example.
The greatest common divisor of (13,5) pair is same as that of (5,3) pair
since 13=(5)2+(3).
But 5=(3)1 +(2)
The greatest common divisor of (5,3) pair is same as that of (3,2) pair
but, 3=(2)1+(1)
The greatest common divisor of (3,2) pair is same as that of (2,1) pair
2=(1)2+(0)
The last divisor for '0 remainder' is gcd. Here it is 1
Following is the output of the algorithm.
2
Figure 1
Figure 2
3
Figure 3
d=EuclidAlgShow(122,84)
(dividend 122) = (divisor 84)*(quotient 1) + (remainder 38)
(dividend 84) = (divisor 38)*(quotient 2) + (remainder 8)
(dividend 38) = (divisor 8)*(quotient 4) + (remainder 6)
(dividend 8) = (divisor 6)*(quotient 1) + (remainder 2)
(dividend 6) = (divisor 2)*(quotient 3) + (remainder 0)
d =
2
function d = EuclidAlgShow(a,b)
% d = EuclidAlgShow(a,b)
%This program performs the Euclidean algorithm, and
shows all intermediate
%steps. It inputs two positive
%integers a, and b, and outputs their greatest
common divisor
%d = gcd(a,b) using the Euclidean algorithm.
aa = max(a,b); bb = min(a,b);
q = floor(aa/bb); r = aa - q*bb;
fprintf('(dividend %d) = (divisor %d)*(quotient
%d) + (remainder %d)\r', aa, bb, q, r)
while r > 0
aa = bb; bb = r; q = floor(aa/bb); r = aa -
q*bb;
fprintf('(dividend %d) = (divisor
%d)*(quotient %d) + (remainder %d)\r', aa, bb, q,
r)
end
d = bb; %The last nonzero remainder.
end
Extended Euclidean Algorithm:
4
Extended Euclidean algorithm finds integer coefficients
x and y such that: ax + by = gcd(a, b)
Figure 4
Algorithm for Extended GCD for (13,5) pair
Extended GCD output for (13,5) pair
Take U=[13 1 0] . We interpret this as 13= 13(1)+5(0)
Take V=[5 0 1] . We interpret this as 5= 13(0) +5(1)
W=U- floor(U(1)/V(1)) * V = [13 1 0]- 2[5 0 1] = [3 1 -2]
[3 1 -2] interpretation is 3=13(1)+5(-2) =13-10
Reassign
5
U=[5 0 1]; V=[3 1 -2]
W=U- floor(U(1)/V(1)) * V = [ 5 0 1]- 1[3 1 -2] = [2 -1 3]
[2 -1 3] interpretation is 2=13(-1)+5(3)= -13+15
Reassign
U=[ 3 1 -2]; V=[2 -1 3]
W=[3 1 -2]-1[2 -1 3]=[1 2 -5]
[1 2 -5] interpreation is 1=13(2)+5(-5)=26-25
Reassign
U=[2 -1 3]; V=[ 1 2 -5]
W=[2 -1 3]-2[1 2 -5]= [0 -5 13]
Reassign
U=[ 1 2 -5] , V=[0 -5 13]
V(1)=0. Since V(1) is zero , the algorithm terminates.
U=[1 2 -5] is the output . That is 1=13(2)+5(-5)
Tha is gcd(13,5)=1 , x=2, y=(-5)
6
Extended GCD output for (106,93) pair
x=EuclidAlgExtShow(106,93);%
Initial Vectors: U = [106, 1, 0], V = [93, 0, 1].
Since V(1) = 93 is positive, we update the vectors:
W = U - floor(U(1)/V(1))*V = [13, 1, -1], U = V = [93, 0, 1], V = W = [13, 1,
Since V(1) = 13 is positive, we update the vectors:
W = U - floor(U(1)/V(1))*V = [2, -7, 8], U = V = [13, 1, -1], V = W = [2, -7,
Since V(1) = 2 is positive, we update the vectors:
W = U - floor(U(1)/V(1))*V = [1, 43, -49], U = V = [2, -7, 8], V = W = [1, 43
Since V(1) = 1 is positive, we update the vectors:
W = U - floor(U(1)/V(1))*V = [0, -93, 106], U = V = [1, 43, -49], V = W = [0,
Since V(1) = 0, the algorithm terminates.
function OutVec = EuclidAlgExtShow(a,b)
% [d x y] = EuclidAlgExt(a,b)
%This program performs the extendedEuclidean
algorithm, with all intermediate
%steps being shown. It inputs two positive
7
%integers a >= b, and outputs their greatest common
divisor
%d = gcd(a,b) along with two integers x and y such
that d = ax + by.
%------------------------------------------
aa = max(a,b); bb = min(a,b);
U = [aa 1 0]; V = [bb 0 1];
fprintf('Initial Vectors: U = [%d, %d, %d], V =
[%d, %d, %d].\r', U, V)
while V(1) > 0
fprintf('Since V(1) = %d is positive, we update
the vectors:\r', V(1))
W = U - floor(U(1)/V(1))*V;
U = V; V = W;
fprintf('W = U - floor(U(1)/V(1))*V = [%d, %d,
%d], U = V = [%d, %d, %d], V = W = [%d, %d,
%d].\r', W,U,V)
end
fprintf('Since V(1) = 0, the algorithm
terminates.\r')
d = U(1); x = U(2); y = U(3);
OutVec = [d x y];
end
%--------------------------------------
Extended GCD for finding Multiplicative Inverse
8
Example: Find multiplicative inverse of 5 under mod 11
% Find multiplicative inverse of 5 under mod 11
a=11; b=5; % If a>b, then multiplicative inverse is
third element in OutVec
X=EGCD(11,5);
disp(mod(X(3),11))
function OutVec = EGCD(a,b)
aa = max(a,b); bb = min(a,b);
U = [aa 1 0]; V = [bb 0 1];
while V(1) > 0
W = U - floor(U(1)/V(1))*V;
U = V; V = W;
end
d = U(1); x = U(2); y = U(3);
OutVec = [d x y];
end
9
10