Two algorithms (Euclid & Stein)

Source: Internet
Author: User

Reprinted from: http://www.cnblogs.com/drizzlecrj/archive/2007/09/14/892340.html

Very old Dongdong, in fact, is not well organized, a lot of information on the Internet, it should be used as a backup to put :-)

1. Euclidean Algorithm and Extended Euclidean Algorithm

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:

Int gcd (int A, int B)
{
If (B = 0)
Return;
Return gcd (B, A % B );
}

Of course, you can also write the iteration form: int gcd (int A, int B)
{
While (B! = 0)
{
Int r = B;
B = A % B;
A = R;
}
Return;
}

In essence, the above principle is used.

Supplement:The Extended Euclidean algorithm is used to solve a group of P in known A and B, so that p * A + Q * B = gcd (A, B) (the solution must exist, according to the related Theorem in number theory ). Extended Euclidean is often used in solving model Linear Equations and equations. The following is an implementation using C ++: int exgcd (int A, int B, Int & X, Int & Y)
{
If (B = 0)
{
X = 1;
Y = 0;
Return;
}
Int r = exgcd (B, A % B, x, y );
Int T = X;
X = y;
Y = T-A/B * Y;

Return R;
}

Comparing this implementation with the Recursive Implementation of GCD, we found that the following x and y values are much greater, which is the essence of extending the Euclidean algorithm.
You can think like this:
For a' = B, B '= A % B, we obtain X and Y to make a' x + B' y = gcd (A', B ')
Because B '= A % B = A-A/B * B (Note: Here/is the division in the programming language)
Then we can get:
A' x + B 'y = gcd (A', B') =>
BX + (a-a/B * B) y = gcd (A', B ') = gcd (a, B) =>
Ay + B (X-A/B * y) = gcd (A, B)
Therefore, for a and B, their corresponding p and q are respectively y and (x-A/B * Y)

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.
(Note:When it comes to discarding division and modulo, the Division can be written as subtraction)

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 common dikes of two even numbers must be divisible by two.

With the above rule, we can give the Stein algorithm as follows:

If a = 0 and B are the largest common divisor, the algorithm ends.
If B = 0, A is the maximum common number, and the algorithm ends
Set a1 = A, B1 = B, and C1 = 1
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)
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)
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)
If neither an nor BN is an even number, an + 1 = | an-Bn |, BN + 1 = min (an, bn), CN + 1 = Cn
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.

A c ++ implementation is provided:

Int gcd (int A, int B)
{
If (A = 0) return B;
If (B = 0) return;
If (a % 2 = 0 & B % 2 = 0) return 2 * gcd (A> 1, B> 1 );
Else if (a % 2 = 0) return gcd (A> 1, B );
Else if (B % 2 = 0) return gcd (a, B> 1 );
Else return gcd (ABS (a-B), min (a, B ));
}

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.

Exercise:
There are not many questions about the naked GCD algorithm on the OJ, most of which are nested in a shell.
I found two answers. Try again.
Hdoj 2028 lowest common multiple plus: this is the minimum public multiple of the number of N (with the maximum public approximate number, the minimum public multiple should be easy)
Zju 2678 bishops on a Toral Board
This topic should be regular, good questions

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.