How is the algorithm so difficult? ---- Use Euclidean algorithm to calculate the maximum common approx. of two numbers

Source: Internet
Author: User
Tags greatest common divisor

I am a newbie. When I was reading a book in the morning, I suddenly saw an example of finding the maximum common number. I suddenly thought that I had read a euclidean algorithm before, therefore, I carefully searched for the principles of the Euclidean algorithm on the Internet. Maybe I haven't looked at the algorithm for a long time, and my mind is getting rusty.

I read a few articles about Euclidean algorithms. Most of them only give formulas, and then I talk a lot about them, then, the formula was introduced. ⊙ B Khan!

After a long time, I turned my head into an impatient one, and finally got a little eye, so I 'd like to share it with you!

1. First of all, the Euclidean algorithm is used to calculate the maximum common divisor of two numbers. You may ask: What is the maximum common divisor?

(When I first saw this problem, I suddenly had a short circuit in my mind, and I didn't know what the maximum public appointment was)

Maximum common divisor:The maximum number that can be divisible by two numbers at the same time.. For example, 8 is the approximate number of 16 and 8, because 16% 8 and 8% 8 are equal to zero! But also 4! Therefore, there will be a lot of common values for the two numbers, but we need to find the largest one!

 

2. Let's use the formula for analysis. Suppose we want to calculate the maximum public approx. of X and Y.

A) First, assume that the maximum common divisor of x> Y, X, and Y is represented by F (x, y ).

B). Suppose x/y =;

X % Y = B;

Therefore, a * Y + B = x

(This can be seen, because a is X divided by the integer part of Y, B is X divided by the remainder part of Y, so a * Y + B = X)

Change the position of the sub-statement above to B = x-A * Y;

Because both X and Y can be divisible by F (x, y) ----- because f (x, y) is the maximum approximate number of X and Y.

Therefore, B can also be divisible by F (x, y) ----- That is, the maximum common divisor f (x, y) of X and Y. It is also the divisor of B, therefore, finding the maximum common divisor of X and Y is equivalent to finding the maximum common divisor of Y and B. (You may ask, why is the greatest common divisor of X and Y converted to the common divisor of B of Y for half a day? Because Y <X, and X % Y must be smaller than Y, so we will narrow down the scope of finding the maximum common approx)

Therefore, the Euclidean formula is f (x, y) = f (y, X % Y );

Therefore, the implementation of this algorithm is non-stop iteration. When x % Y is found to be 0, iteration is stopped, at that time, the maximum approximate number is Y (because X % Y is equal to 0, the maximum approximate number of X and Y is y itself ).

 

3. The above may be a bit cool. You are not surprised. I want to make it clearer! The code implementation section is directly attached below:

Public static int gcd (int x, int y ){
// Prevent program errors due to input 0
If (x = 0 | Y = 0) {return 0 ;}
// Add a judgment guarantee x> Y
If (x <Y ){
Int temp = X;
Temp = y;
Y = X;
}
// Algorithm Implementation
If (X % Y = 0 ){
Return y;
} Else
{
Return gcd (Y, X % Y );
}
}

In fact, there are many other excellent algorithms that we will not mention here. After all, Euclidean algorithms are very efficient. In some companies, this question has also appeared as a pen question. Therefore, the future programmers who are about to find a job after graduation should take a good look! O (∩) O Haha ~

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.