Greatest common divisor explanation
Least common multiple explanation
The greatest common divisor and least common multiple of two numbers are calculated, so long as the greatest common divisor can be obtained least common multiple
Two numbers A and B, assuming greatest common divisor is m,a=a1*m,b=b1*m, least common multiple is a1*b1*m= (a*b)/M
Algorithm one poor lifting method
Judging by the order of 1, 2, 3 ..., the largest number that can be evenly divisible by two numbers is greatest common divisor
Improved
Suppose A<b, press A, A-1, a-2 ... Order, the first one to be divisible by two numbers is greatest common divisor
int getgcd (int x, int y) {int i;for (i=x;;; i--) {if (x%i==0&&y%i==0) break;} return i;}
Algorithm two-way division method (Euclidean algorithm)
First step: Make R A/b derived remainder (0≤R<B)
If r= 0, the algorithm ends; b is the answer.
Step two: Swap, place A←b,b←r, and return to the first step.
int getgcd (int m,int n) {if (m = = 0| | n = = 0) return 0; if (M < n) return GETGCD (n, m); if (m% n = = 0) return n; else return GETGCD (n,m% n);}
Algorithm Shift Subtraction Loss method
The first step: arbitrarily given two positive integers, judging whether they are even. If so, use a 2 reduction, or the second step if not.
The second step: reduce the smaller number by a larger number, and then compare the resulting difference with the smaller number, and subtract the decimal number by the large numbers. Continue this operation until the resulting meiosis and difference are equal.
The product of several 2 and second intermediate numbers in the first step is the desired greatest common divisor.
The "equal number", which is said, is greatest common divisor. The method of seeking "equal number" is "more subtractive". So the subtraction method is also called the equivalence algorithm.
int getgcd (int a,int b) { while (a!=b) { if (a>b) a-=b;else b-=a; } return A;}
The above code just provides ideas that are not validated.
Source: Internal Testing
Greatest common divisor and least common multiple for two numbers