Memory Bread C- Find the greatest common divisor and least common multiple

basic concept

If the number a is divisible by the number b, a is called a multiple of b, and b is called a divisor of a. Both divisors and multiples represent the relationship between an integer and another integer and cannot exist alone. For example, we can only say that 16 is a multiple of a certain number, and 2 is a divisor of a certain number, but we cannot say that 16 is a multiple and 2 is a divisor.
  "Double" and "multiple" are two different concepts. "Multiple" refers to the quotient of the division of two numbers. It can be an integer, decimal, or fraction. "Multiple" is only within the range of divisible numbers, relative to "approximate numbers" In terms of a number, it means a number divisible by a natural number.
  The common divisors of several integers are called the common divisors of these numbers; the largest one is called the greatest common divisor of these numbers. ** For example, the common divisors of 12, 16 are 1, 2, 4, and the largest one is 4, 4 is the greatest common divisor of 12 and 16, ** generally written as (12, 16) = 4. 12, 15 The greatest common divisor of 18 is 3, which is written as (12, 15, 18) = 3.
  Several common multiples of natural numbers are called the common multiples of these numbers, and the smallest natural number is called the least common multiple of these numbers. ** For example: multiples of 4 are 4, 8, 12, 16, ..., multiples of 6 are 6, 12, 18, 24, ..., common multiples of 4 and 6 are 12, 24, ..., the smallest of which It is 12, and ** is generally written as [4, 6] = 12. The least common multiple of 12, 15, 18 is 180. It is written as [12, 15, 18] = 180. The least common multiple of several coprime numbers is the absolute value of their product.
  
Finding the greatest common divisor : Tossing and dividing division
Tossing and dividing: Tossing division is a method of finding the greatest common divisor of two natural numbers, also called Euclidean algorithm.
For example, to find (319,377):
∵ 319 ÷ 377 = 0 (remaining 319)
∴ (319,377) = (377,319);
∵ 377 ÷ 319 = 1 (remaining 58)
∴ (377,319) = ( 319, 58);
∵ 319 ÷ 58 = 5 (remaining 29)
∴ (319,58) = (58,29);
∵ 58 ÷ 29 = 2 (remaining 0)
∴ (58,29) = 29;
∴ (319,377) = 29 .
Can be written in the format on the right.
To find the greatest common divisor of several numbers by tortuous division, you can first find the greatest common divisor of any two of them, then find the greatest common divisor of this greatest common divisor and the third number, and then continue until the last one Count so far. The resulting greatest common divisor is the greatest common divisor of all these numbers.

inline int GCD(int x,int y)
{
    int r=x%y;
    while(r) x=y,y=r,r=x%y;
    return y;
}

Find the least common multiple:
the least common multiple of several numbers is the product of several numbers divided by the greatest common divisor of these numbers.

Published 10 original articles · Like1 · Visits 190

Guess you like

Origin blog.csdn.net/weixin_39475542/article/details/104640223