Calculate the maximum common approx.-Moving Phase Division (Euclidean Algorithm) and Stein Algorithm

Source: Internet
Author: User

From http://www.cppblog.com/aurain/archive/2008/10/08/63480.html

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)

The algorithm is described in C ++ as follows:
Int gcd (int m, int N)
{
If (M = 0)
Return N;
If (n = 0)
Return m;
If (m {
Int TMP = m;
M = N;
N = TMP;
}
While (n! = 0)
{
Int TMP = m % N;
M = N;
N = TMP;
}

Return m;
}

Stein algorithm (see http://blog.vckbase.com/arong/archive/2004/06/15/458.html for the following theory), the code is added by me.

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:

Gcd (A, A) = A, that is, a number and its own common number is its own
Gcd (Ka, KB) = K gcd (a, B), that is, the maximum common approx. Operation and multiplication operation can be exchanged. In particular, when K is 2, it indicates that the maximum number of two even numbers must be divisible by 2.
With the above rule, we can give the Stein algorithm as follows:

1. If a = 0 and B are the largest common divisor, the algorithm ends.
2. If B = 0, A is the maximum public approx. The algorithm ends.
3. Set a1 = A, B1 = B, and C1 = 1
4. if both an and bn are even numbers, An + 1 = An/2, BN + 1 = Bn/2, CN + 1 = Cn * 2 (note, to multiply by 2, you only need to shift the integer to the left, except for 2, you only need to shift the integer to the right)
5. if an is an even number and BN is not an even number, an + 1 = An/2, BN + 1 = BN, CN + 1 = Cn (obviously, 2 is not an odd number)
6. if BN is an even number and an is not an even number, BN + 1 = Bn/2, an + 1 = An, CN + 1 = Cn (obviously, 2 is not an odd number)
7. If neither an nor BN is an even number, an + 1 = | an-Bn |, BN + 1 = min (an, bn), CN + 1 = Cn
8. N ++, to 4
The principle of this algorithm is obvious, so it is no longer proved. Now let's take a look at the difference between the algorithm and Euclidean method in efficiency.

Considering the Euclidean algorithm, the worst case is that each iteration A = 2b-1, so that after iteration, r = B-1. If a is less than 2n, it takes about 4 N iterations. Considering the Stein algorithm, after each iteration, it is clear that an + 1bn + 1 is less than or equal to anbn/2, and the maximum number of iterations cannot exceed 4 N. That is to say, the number of iterations is almost equal. However, it should be noted that for a large prime number, the trial commercial law will make each iteration more complex, so it will be more advantageous for the large prime number Stein.

The algorithm is described in C ++ as follows:
Bool is_even (int n)
{
Return! (N & 1 );
}
Int gcd2 (int m, int N)
{
Int c = 1;
While (M! = 0 & n! = 0)
{
If (is_even (m) & is_even (n ))
{
M> = 1;
N> = 1;
C}
Else if (is_even (m )&&! Is_even (n ))
{
M> = 1;
}
Else if (! Is_even (m) & is_even (n ))
{
N> = 1;
}
Else if (! Is_even (m )&&! Is_even (n ))
{
Int M1 = m;
Int n1 = N;
M = ABS (m-N); // CRT Library Function
N = min (M1, N1); // CRT macro
}
}

Return C * N;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.