[Algorithm] Find the greatest common divisor and the least common multiple (division by turning)

Remarks

2019/7/30 Tuesday
Compulsory third of high school mathematics I learned the "preliminary algorithm". Among them, there is an algorithm called divide by turns. This is a magical algorithm that can be used to find the greatest common divisor of two positive integers. This algorithm can avoid enumerating the divisor of each number, thereby reducing the amount of calculation. Not only in program design, but also in daily life can also use this method to calculate the common divisor.

1. Find the greatest common divisor through division

1. Function introduction

Positive integerGreatest common divisor

2. Principle

The greatest common divisor of the larger number a and the smaller number b of the two numbers is the same as the greatest common divisor of the difference ab and b of the two numbers, so we can considerDivide the larger number by the smaller number to find the quotient and remainder, Repeatedly, the final divisor is the greatest common divisor required. According to the nature of division, this method must be able to find the greatest common divisor after a finite number of steps.

Recently, I accidentally saw a graphical explanation about the principle of tossing and dividing. Quite vividly, the so-called greatest common divisor problem of two numbers can be seen as finding the side length of the largest square that can be completely tiled in a rectangle with length a and width b. And this process is like the game we play on paper when we are bored. We keep using the smaller side to make the largest square to approach the rectangle until there is a small square that can fill the space, and the side length of this small square happens to be The greatest common divisor of a, b. Although it’s a bit magical, this is the charm of mathematicsfigure 1

3. For example

begging8251with6105Greatest common divisor:

  1. 8251 = 6105 × \times × 1 + 2146
  2. 6105 = 2146 × \times × 2 + 1813
  3. 2146 = 1813 × \times × 1 + 333
  4. 1813 = 333 × \times × 5 + 148
  5. 333 = 148 × \times × 2 + 37
  6. 148 = 37 × \times × 4
  7. 37That is8251with6105Greatest common divisor

4. Algorithm analysis

Repeatedly use the larger number to divide the smaller number until it is evenly divided. At this time, the divisor is the greatest common divisor.

5. Algorithm implementation

int a,b,c=1;
scanf("%d%d",&a,&b);
if(a<b) swap(a,b);
while(c){
    
    
	c=a%b;
	a=b;
	b=c;
}
printf("%d",a);

Function version

int gcd(int a,int b){
    
    
	int c=1;
	if(a<b) swap(a,b);
	while(c){
    
    
		c=a%b;
		a=b;
		b=c;
	}
	return a;
}

6. Time complexity

The time complexity of the tossing and dividing method is the log of the larger of the two numbers a and b, which can be regarded as the complexity of O(logn). The specific derivation can be searched by yourself.

2. Least common multiple

When the greatest common divisor is found to be x, pass a × \times× b ÷ \div ÷ x can get the least common multiple.


Welcome to put forward your valuable opinions in the comment area

Guess you like

Origin blog.csdn.net/l1447320229/article/details/97811832