Limitations and Solutions of Euclidean Algorithm for maximum Common Divisor

Source: Internet
Author: User
Tags integer division

1. EuclideanAlgorithmCalculate the maximum common approx.

Finding the maximum common approx. is a relatively basic problem. Euclidean clarified an efficient algorithm as early as in ry, which is said to have occurred around 300 BC. Specifically, assume that the maximum common divisor of X and Y is expressed as f (x, y), and x> = Y> 0. If K = x/y and B = x % Y is selected, x = K * Y + B is converted to B = x-K * Y; if X and Y can be divisible by F (x, y), B can also be divisible by F (x, y ).

F (x, y) = f (y, X % Y ). ApparentlyCodeIt is quite simple, as follows:

Int gcd (int x, int y)

{

Return (! Y )? X: gcd (Y, X % Y );

}

 

2. deformation of the moving phase division

Do you have any problems with the division of moving and moving phases? For example, if you use the above function to calculate GCD (1100100210001,120200021), that is, to calculate the maximum common number for a large integer, the efficiency of the moving phase division becomes a bottleneck. In fact, for a large integer, the overhead of the modulo operation (using Division) is very expensive, which is the limitation of the Euclidean algorithm. How can we optimize it? We can refer to Euclidean's division of the moving phase. Since it is caused by the modulo operation, we don't need to perform the modulo operation. Instead, we use the "-" operation, that is, f (x, y) = f (x-y, Y); Consider it and find that x <Y may occur during algorithm running. In this case, X and Y are exchanged, but the result is not affected. Let's take a look at the code.

Bigint gcd (bigint X, bigint y)

{

If (x <Y) return gcd (Y, X );

Return (! Y )? X: gcd (x-y, y );

}

The bigint used in the Code is a big integer class that can store hundreds of thousands of integers. How can this class be implemented? I will not mention it here. There are a lot of related information on the Internet.ArticleI want to explain the high-precision algorithms. If I read books, I strongly recommend ACM Daniel Liu rujia's book "getting started with algorithm competitions", which is a good introduction to high-precision algorithms.

The big number calculation problem is solved. However, careful readers will find that this algorithm introduces another problem, that is, when x and y differ greatly, the number of iterations of an algorithm is too high, resulting in a low efficiency of the algorithm, such as computing gcd (00000000000001 ). This does exist, so we have to consider other optimizations.

 

3. Use shift operators to optimize the moving phase division

The above two methods are not suitable for solving the big integer problem, and the other is not suitable for solving the problem of too many iterations, so can we combine the two methods to get an acceptable method? The answer is yes. We noted that:

(1) If y = K * Y1, x = K * x1. f (x, y) = K * F (x1, Y1)

(2) If x = p * X2, P is a prime number, and Y % P! = 0, then f (x, y) = f (p * X2, y) = f (X2, Y)

So we get the following solution:

Set P = 2,

If both X and Y are even, f (x, y) = 2 * f (x/2, Y/2) = 2 * f (x> 1, y> 1 );

If X is an even number, Y is an odd number, f (x, y) = f (x/2, y) = f (x> 1, y );

If X is an odd number, Y is European, f (x, y) = f (x, y/2) = f (x, y> 1 );

If both X and Y are odd, f (x, y) = f (y, x-y ). ThenX-y must be an even number, and the next step will be divided by 2.

Therefore, we can see that the time complexity of this algorithm is O (log2 (max (x, y )).

The Code is as follows:

Bigint gcd (bigint X, bigint y)

{

If (x <Y) return gcd (Y, X );

If (y = 0) return X;

Else

{

If (iseven (x ))

{

If (iseven (y ))

Return (gcd (x> 1, Y> 1) <1 );

Return gcd (x >> !, Y );

}

Else

{

If (iseven (y ))

Return gcd (X, Y> 1 );

Return gcd (Y, x-y)

}

}

}

This method cleverly uses the shift operation and the subtraction operation, avoiding the large integer division, and improving the efficiency of the algorithm, which is worth learning. In addition, these three algorithms are implemented by means of tail recursion that can be converted into loops to further improve efficiency. If you are interested, you can implement them on your own.

Let's take a look at some of the lessons learned.

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.