Programming beauty 2.7 Max public approx., Min. Public multiple

Source: Internet
Author: User

The question in the book is to find the maximum common divisor of two numbers. In fact, when we learned the C language, the algorithm that the teacher talked about was a type problem with the prime number in teaching.

The method we learned at that time was "moving phase division", that is, using the formula F (x, y) = f (y, X % Y) until X % Y = 0, X is the maximum common divisor of two numbers.

However, the book says that multiplication and division operations are a waste of time. Therefore, we can think about this problem in another way. multiplication and division cannot be used, so we can only add or subtract values, in the book, if a number can divide X and Y at the same time, it must be able to divide x-y and Y at the same time. Therefore, we define the maximum common divisor of X and Y as f (x, y ), then, based on the above idea, we can get f (x, y) = f (x-y, Y ).

The function declaration is as follows:

int DutTheGreatestCommonDivisor(int, int);

The source code is as follows:

/** The idea here is: if a number can divide X and Y at the same time, it must be able to divide x-y and Y at the same time, therefore, * If we define the maximum common divisor of X and Y as f (x, y), we can obtain f (x, y) = f (x-y, y) */INT dutthegreatestcommondivisor (int m, int N) {/* When M is less than N, two numbers need to be exchanged */If (M <n) return dutthegreatestcommondivisor (n, m);/* WHEN n is equal to 0, the return m is the maximum public approx. */If (n = 0) return m; elsereturn dutthegreatestcommondivisor (m-N, n );}

Now that we have talked about the maximum common number, it corresponds to the minimum common number. When we were in the last few years, we learned a formula. The calculation method of the minimum common number of two numbers is: (x * Y)/maximum common divisor (x, y ).

The function declaration is as follows:

int DutTheLeastCommonMultiple(int, int);

The source code is as follows:

/** The least common multiple can be obtained by using the maximum common approx. Mathematical derivation can look at this: * http://baike.baidu.com/view/341375.htm */INT duttheleastcommonmultiple (int x, int y) {return (x * Y) /dutthegreatestcommondivisor (x, y );}



Programming beauty 2.7 Max public approx., Min. Public multiple

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.