C language: Detailed explanation of the greatest common divisor

C language: Detailed explanation of the greatest common divisor

Hello! Hello, my friends. I have not seen you in a few days. Today I will share with you three methods for finding the greatest common divisor in the C language. Before sharing, let's take a look at what the greatest common divisor is: the greatest common divisor, also known as the greatest common divisor, or the greatest common divisor, refers to the largest divisor of two or more integers. Next, we will share three different methods for finding the greatest common divisor:

1. Toss and to divide

Similarly, adding x, y has the greatest common divisor T, then x, y can be divisible by T. At the same time, it can be found that if we multiply x and y by m respectively, they can still be divisible by T. After adding or subtracting the two numbers, the above situation still holds, that is, C = mx + (±ny), c can be divisible by T. In mathematics (assuming x> y) x/y = a …b, then b = x-a * y; from here we can get that b can also be divisible by T. Therefore, we conclude that if you perform a modulo operation on x and y and assign the result to a larger number, continue the above steps and continue the modulo operation, which will make the value of x and y continue to decrease until two numbers When equal, x%y == 0; at this time, a number that is not 0 in the loop is the greatest common divisor of x and y.
Flow chart
Insert picture description here
code

#include <stdio.h>
#include <windows.h>

#pragma warning(disable:4996)

int maxNum(int num1, int num2)
{
    
    
	while (num1 * num2 != 0)
	{
    
    
		if (num1 > num2)
		{
    
    
			num1 = num1 % num2;
		}
		else if (num1 < num2)
		{
    
    
			num2 = num2 % num1;
		}
		else
		{
    
    
			break;
		}
		return num1 == 0 ? num2 : num1;
	}
}
int main()
{
    
    
	int num1, num2;
	printf("请输入两个整数:");
	scanf("%d%d", &num1,&num2);
	int result = maxNum(num1,num2);
	printf("%d,%d的最大公约数是:%d\n", num1, num2, result);
	system("pause");
	return 0;
}

2. Exhaustive method

As the name implies, it is to use a loop to assign the smaller value of the two numbers x and y to i, divide x by i, and divide y by i. If the remainder of both is 0 at the same time, then i is two Greatest common divisor of the person. If it is not equal to 0, divide i-1, continue to divide x by i, and divide y by i, until the remainder is 0 at the same time.
Flow chart
Insert picture description here
code:

#include <stdio.h>
#include <windows.h>

#pragma warning(disable:4996)

int maxNum(int num1, int num2)
{
    
    
	int i = 0;
	if (num1 > num2)
	{
    
     
		i = num2;
	}
	else
	{
    
    
		i = num1;
	}
	for (; i > 0; i--)
	{
    
    
		if (num1 % i == 0 && num2 % i == 0)
		{
    
    
			return i;
		}
	}
}
int main()
{
    
    
	int num1, num2;
	printf("请输入两个整数:");
	scanf("%d%d", &num1,&num2);
	int result = maxNum(num1,num2);
	printf("%d,%d的最大公约数是:%d\n", num1, num2, result);
	system("pause");
	return 0;
}

3. Toss and toss and subtract

In order to make it easier for everyone to understand this method, we first assume that the greatest common divisor of the numbers x, y is T, it is easy to find that x = T + T +…+ T, y = T + T +…+ T, so when we use When the large number between them is subtracted from the small one, x-y (or y-x) is equivalent to reducing the number of T. Repeat the above steps. When x and y are equal, x-y (or yx) = 0 when the number of T between them is equal, at this time x = y = T;
flow chart:
Insert picture description here
code

#include <stdio.h>
#include <windows.h>

#pragma warning(disable:4996)

int maxNum(int num1, int num2)
{
    
    
	while (1)
	{
    
    
		if (num1 > num2)
		{
    
    
			num1 = num1 - num2;
		}
		else
		{
    
    
			num2 = num2 - num1;
		}
		if (0 == num1 - num2)
		{
    
    
			break;
		}
	}
	return num1;
}
int main()
{
    
    
	int num1, num2;
	printf("请输入两个整数:");
	scanf("%d%d", &num1,&num2);
	int result = maxNum(num1,num2);
	printf("%d,%d的最大公约数是:%d\n", num1, num2, result);
	system("pause");
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_43825377/article/details/105925550