Find LCM algorithm:
LCM = product of two integers gcd
Find GCD algorithm:
(1) Euclidean method
There are two integers a and B:
①a%b to the remainder C
② if c=0, then B is two number of GCD
③ if c≠0, then a=b,b=c, then go back to execute ①
For example, the GCD process for 27 and 15 is:
27÷15 Yu 1215÷12 more than 312÷3 0 so, 3 is GCD
[CPP] view plain copy #include <stdio.h> void main ()/* Euclidean algorithm gcd/{int m, n, a, B, T, C printf ("Input two integer numbers:\n"); scanf ("%d%d", &a, &b); M=a; N=b; while (b!=0)/* The remainder is not 0, continue dividing until the remainder is 0/{c=a%b; a=b; B=c;} printf ("The largest common divisor:%d\n", a); printf ("The least common multiple:%d\n", m*n/a); }
⑵ Phase Subtraction
There are two integers a and B:
① if a>b, then A=a-b
② if a<b, then b=b-a
③ if A=b, then a (or b) is a two-digit GCD
④ if a≠b, then go back to execute ①
For example, the GCD process for 27 and 15 is:
27-15=12 (15>12) 15-12=3 (12>3)
12-3=9 (9>3) 9-3=6 (6>3)
6-3=3 (3==3)
Therefore, 3 is the GCD
[CPP] View Plain copy #include <stdio.h> void main ( ) /* Phase subtraction for GCD */ { int m, n, a, b, c; printf ("input two integer numbers:\n"); scanf ("%d,%d", &a, &b); m=a; n=b; /* a, b are not equal, and large numbers are reduced until they are equal. */ while ( a!=b) if (a>b) a=a-b; else b=b-a; printf ("the largest common divisor:%d\n", a); printf ("the least common multiple:%d\n", m*n/a); }&NBSP;&NBsp
⑶ method of poverty and lifting
There are two integers a and B:
①i=1
② if a,b can be divisible by I at the same time, then t=i
③i++
④ If I <= a (or b), then go back to execute ②
⑤ If i > A (or b), then T is GCD, end
Improved:
①i= A (or b)
② if a,b can be divisible by I at the same time, then I is GCD,
End
③i--, go back and execute ②.
There are two integers a and B:
①i=1
② if a,b can be divisible by I at the same time, then t=i
③i++
④ If I <= a (or b), then go back to execute ②
⑤ If i > A (or b), then T is GCD, end
Improved:
①i= A (or b)
② if a,b can be divisible by I at the same time, then I is GCD,
End
③i--, go back and execute ②.
[CPP] View plain copy #include <stdio.h> void main () /* to seek GCD */ { int m by exhaustive method, &NBSP;N,&NBSP;A,&NB