C language realizes the greatest common divisor's toss and turns division and more subtraction

Toss and turns and divide: find (A,B)the greatest common divisor of two numbers , the next result is gcd(B,A%B), if A%B==0, then B is the greatest common divisor.
 
 
Example: Find (240, 380):

  1. 240÷380=0 (remaining 240) → (380, 240)
  2. 380÷240=1 (remaining 140) → (240,140)
  3. 240÷140=1 (remaining 100) → (140, 100)
  4. 140÷100=1 (remaining 40) → (100, 40)
  5. 100÷40=2 (remaining 20) → (40, 20)
  6. 40÷20=2 (remaining 0) → (20, 0)
    So the greatest common divisor of (240, 380) is: 20.
     
     

More subtraction method:

  1. If the two numbers are equal, the greatest common divisor is that number.
  2. One odd and one even, two odd: subtract the smaller number from the larger number, and then compare the difference with the smaller number. If they are not equal, assign the minus to the minus and the difference to the minus. If they are equal, they are not executed, and the difference is the greatest common divisor.
  3. Two even: Divide two numbers at the same time, and record the number of times n until a certain number is no longer even. Then, subtract the smaller number from the larger number, and then compare the difference with the smaller number. If it is not equal, assign the minus to the minus and the difference to the minus, and continue the above operation. If they are equal, they are not executed , and the greatest common divisor is 差*pow(2,n).

 
 
Example: One odd and one even (98, 63)

  1. 98-63=35
  2. 63-35=28
  3. 35-28=7
  4. 28-7=21
  5. 21-7=14
  6. 14-7=7 (Subtraction and difference are equal, no need to continue execution)

So the greatest common divisor of (98, 63) is: 7.
 
 
Example: Two pairs (240, 100)

  1. 240÷2=120,100÷2=50 →n=1
  2. 120÷2=60, 50÷2=25 → n=2 (25 is not an even number at this time, no need to continue to divide by 2)
  3. 60-25=35
  4. 35-25=10
  5. 25-10=15
  6. 15-10=5
  7. 10-5=5 (the subtraction and the difference are equal, no need to continue execution)

Therefore, the greatest common divisor of (240, 100) is: 5*pow(2, 2) = 20.
 
 
 
 
 
Code:

#include <stdio.h>
#pragma warning(disable:4996)
#include <math.h>
int Gcd1(int x, int y) // 辗转相除
{
    
    
	if (0 == y)
	{
    
    
		return x;
	}
	else
	{
    
    
		return Gcd1(y, x%y);
	}
}
int Gcd2(int x, int y)//更相减损  
{
    
    
	int t = 0;
	int r = 0;
	int n = 0;
	if (x == y)
	{
    
    
		return x;
	}
	else
	{
    
    
		while (1)
		{
    
    
			if (x % 2 != 0 || y % 2 != 0)
			{
    
    
				break;
			}
			x = x / 2;
			y = y / 2;
			n = n + 1;
		}
		while (1)
		{
    
    
			if (x < y)
			{
    
    
				r = x;
				x = y;
				y = r;
			}
			t = x - y;
			x = y;
			y = t;
			if (x == t)
			{
    
    
				break;
			}
		}
		if (n != 0)
		{
    
    
			x = x * pow(2,n);
		}
		return x;
	}
}
int main()
{
    
    
	int a = 0;
	int b = 0;
	int c = 0;
	int d = 0;
	printf("input two numbers:");
	scanf("%d %d", &a, &b);
	printf("%d和%d的最大公约数为:%d\n", a, b, Gcd1(a, b));
	printf("input two numbers:");
	scanf("%d %d", &c, &d);
	printf("%d和%d的最大公约数为:%d\n", c, d, Gcd2(c, d));
	return 0;
}

Guess you like

Origin blog.csdn.net/w903414/article/details/105326369