# 2.7 beauty of programming-three solutions to the maximum common divisor [efficient method to solve gcd problem], beauty of Programming

Source: Internet
Author: User

2.7 beauty of programming-three solutions to the maximum common divisor [efficient method to solve gcd problem], beauty of Programming

Http://www.cnblogs.com/hellogiser/p/efficient-method-to-solve-gcd-problem.html

[Question]

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.

[Solution]

[1. Division of Moving Phase]

Division: f (x, y) = f (y, x % y) (x> y)

F () = 6

[Code]

C ++ Code
 12345678910111213141516 /*Version: 1.0Author: hellogiserBlog: http://www.cnblogs.com/hellogiserDate: 2014/7/8*/Int gcd (int x, int y){If (x

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.

[2. Moving and subtraction]

Moving and subtraction: f (x, y) = f (y, x-y) (x> y)

F () = f (6) = 6

[Code]

C ++ Code
 12345678910111213141516 /*Version: 1.0Author: hellogiserBlog: http://www.cnblogs.com/hellogiserDate: 2014/7/8*/Int gcd (int x, int y){If (x

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 a situation occurs (1 000 000 000, 1), it is quite depressing.

[3. Parity method]

Parity method:

This method combines solution 1) and solution 2 to reduce the computing complexity and the number of iterations.

1: If both x and y are even, 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 ))).

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

= 2 * f (15, 6)

= 2 * f (15, 3)

= 2 * f (3, 12) = 2 * f (12, 3)

= 2 * f (6, 3)

= 2 * f (3, 3)

= 2 * f (3, 0)

= 2*3

= 6

Code]

C ++ Code
 123456789101112131415161718192021222324252627282930313233343536 /*Version: 1.0Author: hellogiserBlog: http://www.cnblogs.com/hellogiserDate: 2014/7/8*/Bool IsEven (int x){Return (x & 0x1) = 0;}Int gcd (int x, int y){If (x 1, y> 1 );Else // case 2, x is even, y is oddReturn gcd (x> 1, y );}Else{If (IsEven (y) // case 3, x is odd, y is evenReturn gcd (x, y> 1 );Else // case 4, x, y are all oddReturn gcd (y, x-y );}}}

[Reference]

Http://blog.csdn.net/ajioy/article/details/7478008

Http://blog.csdn.net/rein07/article/details/6739688

Http://www.cnblogs.com/hellogiser/p/efficient-method-to-solve-gcd-problem.html

Related Keywords:
Related Article

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.