Step-by-step write algorithm (greatest common divisor, least common multiple)

Source: Internet
Author: User
Tags greatest common divisor

Original: Step by step writing algorithm (greatest common divisor, least common multiple)

"Disclaimer: Copyright, welcome reprint, please do not use for commercial purposes. Contact mailbox: feixiaoxing @163.com "


Solving least common multiple and greatest common divisor is a topic that we often need to practice when we start programming. From the surface, it seems that we need to solve the problem is two, but in fact is a topic. That's greatest common divisor? Why is it? We can imagine these two numbers m and N, assuming that the greatest common divisor of M and N is a. So we can write this:

m = b *a;

n = c * A;

So the least common multiple of M and N should be a*b*c Ah, that's not M * N/A, where M and N are known, and A is the greatest common divisor that needs to be solved. So we have the following code,

int getmincommonmultiple (int m, int n) {assert (M && n); return m * N/getmaxcommondivide (M, n);}
The solution of greatest common divisor can be started below. In fact, about the solution of greatest common divisor, we see more is a variety of shortcuts, such as Euclid. Euclid's idea was ingenious, and it took advantage of the important condition that the greatest common divisor between m, N and N and m%n were equal, taking full advantage of the substitution method, and obtaining our greatest common divisor at the moment m%n equals 0. However, we usually can think of the method is not many, the following method is a typical one:

A) first to the data m and n size, small assignment to smaller, large assignment to larger

b) index index from 2 to smaller traversal, found that there is no data can be evenly divisible by both, and then update the data

c) The maximum number of conventions to be obtained after the end of the cycle

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 have no problem, but still left a regret, if the data of M and N are more than 32 bits, then what? Some friends might say that there are now 64 bits of CPU. But what if we don't have a 64-bit CPU at the moment?


Summarize:

(1) Seemingly greatest common divisor, least common multiple is a small problem, but to write a very easy, algorithm, parameter judgment, logic should pay attention to,

(2) If the data of M and N are relatively large, it is possible to use multi-core to reduce the computational complexity,

(3) If the data in M and N is greater than 32 bits, what should I do?

(4) Small problems seem simple, but in different scenarios can become very complex, as a face test can fully examine the interviewer's communication skills and adaptability.



Step-by-step write algorithm (greatest common divisor, least common 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.