C practical exercise topic 16 – greatest common divisor and least common multiple


Title:Enter two positive integers m and N to find the greatest common divisor and the least common multiple.

Program analysis:

(1) The least common multiple = the product of two input numbers divided by their greatest common divisor, the key is to find the greatest common divisor;

(2) To find the greatest common divisor by rolling and dividing (also known as Euclidean algorithm)

1) Let C be the greatest common divisor of a and B, denoted as C = GCD (a, b), a > = B,
Let r = a mod B
Let a = KC, B = JC, then K, J are prime, otherwise C is not the greatest common divisor
According to it, r = a-mb = KC MJC = (k-mj) C
It can be seen that R is also a multiple of C, and k-mj and j are mutually prime, otherwise, it is in contradiction with the mutual prime of K and J,
Therefore, the greatest common divisor of B and R is also C, that is GCD (a, b) = GCD (B, a, mod, b).

2) Algorithm description:

Step 1: a △ B, let R be the remainder (0 ≤ R)Step 2: Exchange: set a ← B, B ← R, and return to the first step.


 int main()
     int a,b,t,r,n;
     Printf ("please enter two numbers:;
     scanf("%d %d",&a,&b);

The output results of the above examples are as follows:

Please enter two numbers:
12 26
The greatest common divisor of these two numbers is 2 and the least common multiple is 156

Thank you for your reading, please feel with your heart! Hope to help you who love learning!! Sharing is also a kind of happiness!!! Please relay…

Click to view the original text, thank you!