The maximum appointment of study notes in the beauty of Programming

Source: Internet
Author: User

Description ):

Calculate the maximum common divisor (GCD) of two positive integers ). If both integers are large, is there any simple algorithm?

For example, given two numbers 1 100 100 210 001,120 200 021, the maximum number of public approx is obtained.

 

Analysis and Solution:

As early as around 300 AD, Euclidean gave an efficient solution in his book ry, Division of moving phase. The principle is very simple and subtle. Assume that f (x, y) is used to represent the maximum public approx. k = x/y, B = x % Y, then x
= Ky + B. If a number can divide X and Y at the same time, it must be able to divide B and Y at the same time, while the number that can divide B and Y at the same time must be able to divide X and Y at the same time, that is to say, the common divisor of X and Y is the same as that of B and Y, and the maximum common divisor is the same, then f (x, y)
= F (y, X % Y) (x> = Y> 0), you can convert the original problem to the maximum public approx. Of two decimal places until one of them is 0, the other remaining number is the largest common divisor of the two.

Example:

F () = f () = 6

 

Solution 1:

The simplest method is to use code directly to implement the division of the moving phase. The modulo operation is used in this method. For large integers, the overhead of the modulo operation (Division) is very expensive and will become the bottleneck of the entire algorithm.

The recursive version code is as follows:

int gcd(int x,int y){   return (!y) ? x : gcd(y, x % y);}

Non-recursive version:

Int gcd (int x, int y) {int I = 1; // The initial value is not 0 while (I! = 0) {I = x % Y; X = y; y = I;} return X ;}

Solution 2:

The preceding analysis is similar to the moving phase division method. If a number can divide X and Y at the same time, it is necessary to divide x-y and Y at the same time; however, the number that can divide x-y and Y at the same time must be able to divide X and Y at the same time, that is, the number of M-grams of X and Y is the same as that of x-y and Y, the maximum common divisor is the same, that is, f (x, y) = f (x-y, Y); then the modulo operation of large integers is not required, convert to a much simpler subtraction of large integers. In actual operations, if X <Y, you can switch (x, y) First, because f (x, y) = f (y, x ), this avoids the occurrence of a positive number and a negative maximum common number. Iteration continues until one of them is 0.

Example:

// Bigint is a self-implemented bigint gcd (bigint X, bigint y) {If (x <Y) return gcd (Y, X ); if (y = 0) return X; else return gcd (x-y, Y); // gcd (Y, x-y) may be more suitable}

The bigint class must overload the subtraction operator "-". This algorithm removes the hassle of division by large integers, but it also has shortcomings. The biggest bottleneck is that there are too many iterations. If such situations occur (10 000 000 000 000, 1), it is quite depressing.

 

Solution 3:

This method combines solution one and Solution Two to reduce the computing complexity and the number of iterations.

Start with analyzing the features of the common number.

For Y and X, if y = K * Y1, x = K * X1. Then f (y, x) = K * f (y, X ).

In addition, if X = p * X1, assume that p is a prime number and Y % P! = 0, then f (x, y) = f (p * X1, y) = f (x1, Y ).

After the preceding two points are noted, we can improve the algorithm based on these two points. The simplest method is that 2 is a prime number, and for a large integer expressed in binary, it is easy to convert divided by 2 and multiplied by 2 into a shift operation, to avoid the division of large integers, we can use 2 for analysis.

Take P = 2;

1: If
X, Y are even numbers, f (x, y) = 2 * f (x/2, Y/2) = 2 * f (x> 1, Y> 1)

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

3: If X is odd, Y is even, f (x, y) = f (x, y/2) = f (x, y> 1)

4: If X and Y are all odd, f (x, y) = f (y, x-y)

After F (x, y) = f (y, x-y), (x-y) is an even number, and the next step is divided by 2.

Therefore, in the worst case, the time complexity is O (log2 (max (x, y )));

Consider the following:

F (42, 30) = 2 * F (21, 15) 4

= 2 * F (15, 6) 3

= 2 * F (15,3) 4

= 2 * F (3, 12) 3

= 2 * F (3, 6) 3

= 2 * F (3, 3) 4

= 2 * F (3, 0)

= 2*3

= 6

The Code is as follows:

// Bigint is a big integer class implemented by itself. iseven () determines whether it is an even 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) // Case 1, x, Y is an even return 2 * gcd (x> 1, Y> 1); else // Case 2, X is an even, Y is an odd return gcd (x> 1, y);} else {If (iseven (y) // case 3, X is odd, Y is even return gcd (X, Y> 1 ); else // case 4, x, y are all odd return gcd (Y, x-y );}}}

 

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.