1. 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: G (A, B) = g (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) have the same common number, and the maximum common number must be equal.
 
2. Stein Algorithm
The euclidean algorithm is a traditional algorithm used to calculate the maximum common divisor of two numbers. It is both theoretical and efficient. But he has a fatal defect that is only apparent when there is a large prime number.
 
Consider the current hardware platform. Generally, an integer can be up to 64 bits. For such an integer, it is very easy to calculate the Modulus between two numbers. For a 32-bit platform, to compute two integers of no more than 32 bits, only one instruction cycle is required. To compute an integer model of no more than 64 bits, there are only a few cycles. However, for a larger prime number, this calculation process has to be designed by the user. To calculate two integers that exceed 64 bits, users may have to adopt trial and commercial law similar to the multi-digit Division manual calculation. This process is not only complex, but also consumes a lot of CPU time. Modern cryptographic algorithms require the calculation of prime numbers of more than 128 bits. The design of such a program is eager to discard division and modulo.
 
The Stein algorithm was proposed by J. Stein in 1961. This method is also used to calculate the maximum common divisor of two numbers. Unlike the Euclidean algorithm, the Stein algorithm only supports integer shift and addition and subtraction, which is a good news for programmers.
 
To illustrate the correctness of the Stein algorithm, we must first note the following conclusions:
 
G (A, A) = A, that is, a number and its own common number is its own
G (Ka, KB) = K g (a, B), that is, the maximum common number operation and multiplication operation can be exchanged. In particular, when k = 2, it indicates that the maximum number of common dikes of two even numbers must be divisible by two.
 
The complete implementation code of the algorithm is as follows (C ++)
 
 
/*************************************** **************************** 
 
File Name: G. cpp
Abstract: The implementation of Euclidean Algorithm and Stein Algorithm for Finding the maximum common approx.
Development Platform: Win XP SP2
Compiling environment: cl.exe 8.0 (in Visual Studio 2005 SDK)
By: 88250
Completion date: Version: 1.0
Blog: http://DL88250.ynutx.net
E-mail: DL88250@gmail.com
QQ: 845765 or 316281008
**************************************** ***************************/
 
# Include <iostream>
 
Using namespace STD;
 
Int Euclid (int A, int B)
{
If (0 =)
{
Return B;
}
If (0 = B)
{
Return;
}
If (A> B)
{
Int T =;
A = B;
B = T;
}
For (INT c = A % B; C> 0; C = A % B)
{
A = B;
B = C;
}
Return B;
}
 
Int Stein (int A, int B)
{
Int AA [255] = {0 };
Int Ba [255] = {0 };
Int Ca [255] = {0 };
AA [0] =;
Ba [0] = B;
Ca [0] = 1;
For (INT I = 0; I <255; I ++)
{
If (0 = AA [I])
{
Return Ba [I];
}
If (0 = BA [I])
{
Return AA [I];
}
If (0 = (AA [I] & 1) & (0 = (BA [I] & 1 )))
{// All are even numbers
AA [I + 1] = AA [I]> 1;
Ba [I + 1] = BA [I]> 1;
Ca [I + 1] = Ca [I] <1;
}
If (0 = (AA [I] & 1) & (1 = (BA [I] & 1 )))
{// An is an even number, and BN is not
AA [I + 1] = AA [I]> 1;
Ba [I + 1] = BA [I];
Ca [I + 1] = Ca [I];
}
If (0 = (BA [I] & 1) & (1 = (AA [I] & 1 )))
{// BN is an even number, and an is not
Ba [I + 1] = BA [I]> 1;
AA [I + 1] = AA [I];
Ca [I + 1] = Ca [I];
}
If (1 = (BA [I] & 1) & (1 = (AA [I] & 1 )))
{// Not even
AA [I + 1] = ABS (BA [I]-aa [I]);
Ba [I + 1] = AA [I] <Ba [I]? AA [I]: BA [I];
Ca [I + 1] = Ca [I];
}
}
}
 
Int main (INT argc, char * argv [])
{
Cout <Euclid (50, 25) <Endl;
Cout <Stein (50, 25) <Endl;
 
Return 0;
}
 
 
 
C