Euclidean Algorithm
The euclidean algorithm, also known as the moving phase division, is used to calculate the maximum approximate number of two integers A and B. The computation principle depends on the following theorem:
Theorem: gcd (a, B) = gcd (B, A mod B)
Proof: A can be expressed as a = kb + R, then R = a mod B
Assume that D is a common divisor of A and B.
D | a, d | B, and r = A-kb, so d | r
Therefore, D is the common divisor of (B, A mod B ).
Assume that D is the common divisor of (B, A mod B ),
D | B, d | r, but a = kb + R
Therefore, D is also the common divisor of (a, B ).
Therefore, (A, B) and (B, A mod B) share the same common number, and the maximum common number must be equal.
The euclidean algorithm is based on this principle. Its algorithm is described in the C ++ language as follows:
void swap(int & a, int & b)
{
int c = a;
a = b;
b = c;
}
int gcd(int a,int b)
{
if(0 == a )
{
return b;
}
if( 0 == b)
{
return a;
}
if(a > b)
{
swap(a,b);
}
int c;
for(c = a % b ; c > 0 ; c = a % b)
{
a = b;
b = c;
}
return b;
}
Modulo p multiplication inverse element
For integers A and P, if integer B is satisfied with AB mod p = 1, B is the model p multiplication inverse element of.
Theorem: the necessary and sufficient condition for a multiplication inverse element with mod P is gcd (A, P) = 1.
Proof:
First prove adequacy
If gcd (A, P) = 1, according to Euler's theorem
Obviously, a Phi (P)-1 mod p is the inverse element of the p multiplication mode of.
Prove the necessity
Assume that the multiplication inverse element of model a p is B.
AB 1271 mod p
Then AB = KP + 1, so 1 = AB-kP
Because gcd (A, P) = d
So d | 1
So D can only be 1
Extended Euclidean Algorithm
The Extended Euclidean algorithm can not only calculate (a, B) the maximum common approx. It can also calculate the multiplication inverse elements of a Mode B and B mode A. It is described in the C language as follows:
Int gcd (int A, int B, Int & AR, Int & Br)
{
Int x1, x2, X3;
Int Y1, Y2, Y3;
Int T1, T2, T3;
If (0 =)
{// If the number of values is 0, there is no multiplication inverse element.
AR = 0;
BR = 0;
Return B;
}
If (0 = B)
{
AR = 0;
BR = 0;
Return;
}
X1 = 1;
X2 = 0;
X3 =;
Y1 = 0;
Y2 = 1;
Y3 = B;
Int K;
For (t3 = X3 % Y3; T3! = 0; T3 = X3 % Y3)
{
K = X3/Y3;
T2 = x2-K * Y2;
T1 = x1-K * Y1;
X1 = Y1;
X1 = Y2;
X3 = Y3;
Y1 = T1;
Y2 = t2;
Y3 = T3;
}
If (Y3 = 1)
{
// There is a multiplication inverse element
AR = Y2;
BR = x1;
Return 1;
} Else {
// The number of common approx is not 1, and there is no multiplication inverse element.
AR = 0;
BR = 0;
Return Y3;
}
}
The Extended Euclidean algorithm is consistent with the general Euclidean Algorithm for the calculation of the maximum common divisor. It is difficult to understand the inverse element of multiplication calculation. I thought about how to prove him in half an hour.
First, repeat the Division statement:
If gcd (a, B) = D, m, n exists, so that D = MA + Nb. This relationship is called the integer d, m, N is called the combination coefficient. When d = 1, there is Ma + NB = 1. At this time, we can see that m is the multiplication inverse element of a Mode B, and N is the multiplication inverse element of B mode.
In order to prove the above conclusion, we regard XI and Yi in the above calculation as the initial iteration values of Ti, evaluate a group of numbers (T1, T2, T3), and use induction to prove: after the Euclidean algorithm is extended, each row meets the requirements of a × t1 + B × t2 = T3.
Row 1: 1 x A + 0 x B =
Row 2: 0 × A + 1 × B = B
Assume that the first K rows are true, and check row k + 1.
For K-1 rows and K rows there are
T1 (k-1) T2 (k-1) T3 (k-1)
T1 (k) T2 (k) T3 (k)
Meet the following requirements:
T1 (k-1) × A + T2 (k-1) × B = T3 (k-1)
T1 (k) × A + T2 (k) × B = T3 (k)
Based on the Extended Euclidean algorithm, we assume that T3 (k-1) = J T3 (k) + R
Then:
T3 (k + 1) = r
T2 (k + 1) = t2 (k-1)-J × T2 (k)
T1 (k + 1) = T1 (k-1)-J × T1 (k)
Then
T1 (k + 1) × A + T2 (k + 1) × B
= T1 (k-1) × A-J × T1 (k) × A +
T2 (k-1) × B-J × T2 (k) × B
= T3 (k-1)-J T3 (K) = r
= T3 (k + 1)
Pass
Therefore, when the final T3 iteration is calculated to 1, T1 × A + T2 × B = 1. Obviously, T1 is the multiplication inverse element of a modulo B, t2 is the multiplication inverse element of model B.