Basic concepts
- Least common multiple: multiples of two or more integers that are public are called their common multiple. The least common multiple of the integer A, a, and the same as [A, b], the a,b,c least common multiple is recorded as [A,b,c], and the least common multiple of multiple integers have the same notation.
- Greatest common divisor: Also known as greatest common divisor, the largest common factor, refers to two or more integers total of the largest one. The greatest common divisor of A, B is recorded as (A, b), the same as the greatest common divisor of a,b,c (A,B,C), and the greatest common divisor of multiple integers have the same notation.
- With regard to least common multiple and greatest common divisor, we have such a theorem: (A, b) [A,b]=ab (A, b are integers).
Method Analysis Greatest Common divisor
- Euclidean method:
Set two to A, B (a≥b), and the steps for A and B greatest common divisor are as follows:
①a%b remainder C
② if c=0, then B is a two-digit greatest common divisor
③ if c≠0, then a=b,b=c, then go back to execute ①
- More subtractive methods:
The steps for A and B greatest common divisor are set at two for A and B, as follows:
① if a>b, then A=a-b
② If a
Least common multiple
- (A, B) [A,b]=ab
The first calculation of AB and [A, b] Division is least common multiple.
- Poor lifting method:
Set two numbers to A, B (a≥b), t=a,i=1 the steps for a and b least common multiple are as follows:
①a%b remainder C
② if c=0, then A is a two-digit least common multiple
③ if c≠0, then i=i+1,a=t*i, then go back to execute ①
Code implementation Greatest common divisor
- Euclidean method:
int HCD(int x, int y){int temp;if (x < y) //如果x<y交换x,y{temp = x;x = y;y = temp;}while (x%y) //x%y!=0时 {temp = x; x = y; //将y赋给xy = temp % y; //余数赋给y}//直到x%y == 0时y为最大公约数return y;}
- More subtractive methods:
int HCD(int x, int y){ int MAX = x > y ? x : y; int MIN = x < y ? x : y; int TEMP = MAX - MIN; if (TEMP == 0) return MAX; //递归终止 else HCD(TEMP, MIN); //递归}
Least common multiple
- (A, B) [A,b]=ab
//求最大公约数 辗转相除法int HCD(int x, int y){int temp;if (x < y) //如果x<y交换x,y{temp = x;x = y;y = temp;}while (x%y) //x%y!=0时 {temp = x;x = y; //将y赋给xy = temp % y; //余数赋给y}//直到x%y == 0时y为最大公约数return y;}//求最小公倍数(a,b)[a,b]=abint ICM2(int x, int y){return x*y / HCD2(x, y);}
- Poor lifting method:
//求最小公倍数int ICM(int x, int y){int temp;int i = 1;if (x < y) //如果x<y交换x,y{temp = x;x = y;y = temp;}temp = x;while (x%y) //x%y!=0时{i++;x = temp * i;//将x*1、x*2...赋给x}//直到x%y == 0时x为最小公倍数return x;}
Reference article:
- Common algorithm: C language to seek least common multiple and greatest common divisor three kinds of algorithms--csdn
- C Language Computing 2 numbers of least common multiple--blog Park
- Baidu Encyclopedia
C language--seeking greatest common divisor and least common multiple