One-step write algorithm (maximum common divisor and least common multiple)

Source: Internet
Author: User

 

[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]

 

 

 

 

Solving the least common multiples and the maximum common divisor is a topic we often need to exercise when programming. From the question point of view, it seems that we need to solve two questions, but they are actually one question. That is to find the maximum public approx? Why? We can assume the two numbers m and n, and assume that the maximum common divisor of m and n is. We can write as follows:

 

M = B *;

 

 

N = c *;

 

Therefore, the minimum public multiples of m and n should be a * B * c. That is not m * n/a, where m and n are known, and a is the maximum common number to be solved. So we have the following code,

 

Int GetMinCommonMultiple (int m, int n)

{

Assert (m & n );

 

Return m * n/GetMaxCommonDivide (m, n );

}

Int GetMinCommonMultiple (int m, int n)

{

Assert (m & n );

 

Return m * n/GetMaxCommonDivide (m, n );

} You can start solving the maximum common divisor. As a matter of fact, we can see a variety of shortcuts for solving the maximum common divisor, such as the Euclidean method. The Euclidean method is a clever concept. It utilizes this important condition that the maximum common divisor between m, n, n, and m % n is equal and fully utilizes the replacement method, at the moment that m % n is equal to 0, we can obtain our maximum public approx. However, there are not many methods we can think of at ordinary times. The following method is a typical one:

 

A) First, judge the size of Data m and n. assign a small value to smaller, and assign a large value to larger.

 

B) index traversal starts from 2 to smaller. If any data can be divided by both, update the data.

 

C) obtain the maximum number of common appointments after the loop ends.

 

Int GetMaxCommonDivide (int n, int m)

{

Int index;

Int smaller;

Int larger;

Int value;

Assert (n & m );

 

If (n> m ){

Larger = n;

Smaller = m;

} Else {

Larger = m;

Smaller = n;

}

 

Value = 1;

For (index = 2; index <= smaller; index ++ ){

If (0 = (smaller % index) & 0 = (larger % index ))

Value = index;

}

 

Return value;

}

Int GetMaxCommonDivide (int n, int m)

{

Int index;

Int smaller;

Int larger;

Int value;

Assert (n & m );

 

If (n> m ){

Larger = n;

Smaller = m;

} Else {

Larger = m;

Smaller = n;

}

 

Value = 1;

For (index = 2; index <= smaller; index ++ ){

If (0 = (smaller % index) & 0 = (larger % index ))

Value = index;

}

 

Return value;

} The above Code seems to be okay, but it leaves a regret. What if the data of m and n is greater than 32 bits? Some may say that there is a 64-bit cpu. But what if we don't have a 64-bit cpu at the moment?

 

 

 

 

Summary:

 

(1) it seems that the maximum public approx. And the minimum public multiples are a small problem, but it is difficult to write. You should pay attention to algorithms, parameter judgment, and logic,

 

(2) Is it possible to use multiple cores to reduce the computing complexity if both m and n data are large,

 

(3) What if data in m and n is greater than 32 bits?

 

(4) small questions seem simple, but they can become very complex in different scenarios. As interview questions, we can fully examine the communication and adaptability of the interviewees.

 

Related Article

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.